admin管理员组文章数量:1316399
I've built a filterable post listing where the user can filter based on two taxonomies, this is working but I now want to add a third taxonomy to the filter, but I think the logic I'm using isn't very efficient.
Here's my code for the two taxonomy filter. If I was to add a third taxonomy using this approach I would need to add an If/Else block for each combination, but there must be a better way of doing this?
function filter_ajax() {
$category = $_POST['category'];
$sector = $_POST['sector'];
$size = $_POST['size'];
$args = array(
'post_type' => 'project',
'post_status' => 'publish',
'posts_per_page' => -1
);
if(isset($category) && isset($sector)) {
$args["tax_query"] = array(
'relation' => 'AND',
array(
'taxonomy' => 'project_category',
'terms' => array($category)
),
array(
'taxonomy' => 'sector',
'terms' => array($sector)
)
);
} else if(isset($sector)) {
$args["tax_query"] = array(
array(
'taxonomy' => 'sector',
'terms' => array($sector)
)
);
} else if (isset($category)) {
$args["tax_query"] = array(
array(
'taxonomy' => 'project_category',
'terms' => array($category)
)
);
}
I'm passing in the term id for the taxonomies: category, sector and size. Sometimes these will be unset if the user hasn't selected a term in a particular taxonomy.
Is there a better way of structuring this request without having to add more if/else statements?
本文标签: wp queryFiltering posts based on three taxonomies
版权声明:本文标题:wp query - Filtering posts based on three taxonomies 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742001904a2411184.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论