admin管理员组文章数量:1390258
I have the following arguments for a WP_Query:
$args = array(
'post_type' => array( 'cpt-1', 'cpt-2' ),
'post_status' => 'publish',
'posts_per_page' => 9,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'key-1',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'key-1',
'value' => 1,
'compare' => '!='
)
),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'ctax-1',
'field' => 'slug',
'terms' => 'term-1',
'operator' => 'NOT IN'
),
array(
'taxonomy' => 'ctax-2',
'field' => 'slug',
'terms' => 'term-2',
'operator' => 'NOT IN'
)
),
);
Now I would like to add to the loop the custom post type cpt-3
but only if its posts has the custom field value of the custom meta key-2
set to 1.
Is it possible?
I have the following arguments for a WP_Query:
$args = array(
'post_type' => array( 'cpt-1', 'cpt-2' ),
'post_status' => 'publish',
'posts_per_page' => 9,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'key-1',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'key-1',
'value' => 1,
'compare' => '!='
)
),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'ctax-1',
'field' => 'slug',
'terms' => 'term-1',
'operator' => 'NOT IN'
),
array(
'taxonomy' => 'ctax-2',
'field' => 'slug',
'terms' => 'term-2',
'operator' => 'NOT IN'
)
),
);
Now I would like to add to the loop the custom post type cpt-3
but only if its posts has the custom field value of the custom meta key-2
set to 1.
Is it possible?
Share Improve this question edited Feb 21, 2018 at 10:11 Johansson 15.4k11 gold badges43 silver badges79 bronze badges asked Feb 21, 2018 at 10:02 MKayMKay 1791 silver badge11 bronze badges 1- Don't you want to modify the query instead? – cybmeta Commented Feb 21, 2018 at 10:08
1 Answer
Reset to default 1If the key-2
is uniquely assigned to cpt-3
post type, then you can add another meta query argument to your query arguments:
array(
'key' => 'key-2',
'value' => 1,
'compare' => '='
)
If not, you can run another query, and then merge them as follows:
$final_query = array_merge( (array) $query_1, (array) $query_2 );
But, you should use get_posts()
instead of WP_Query()
since you can't merge objects with methods. The get_posts()
returns an array of posts which can be merged.
However you should notice, meta queries are expensive and will slow down your website ( probably ).
本文标签: wp queryWPQuery include custom post type only with specific meta value
版权声明:本文标题:wp query - WP_Query: include custom post type only with specific meta value 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744580849a2613911.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论