admin管理员组文章数量:1292078
So here is my problem I want to show Posts if have same category if not have post then show post of same tag. And to do that I am using "pre_get_posts" action. and setting query like following.
function related_custom_posts($query){
$query->set( 'category__in', array(2,3) );
$query->set( 'tag__in', array(10,13) );
}
add_action( 'pre_get_posts', 'related_custom_posts', 1 );
But it create sql like this
AND ( wp_term_relationships.term_taxonomy_id IN (2) AND tt1.term_taxonomy_id IN (11) )
but i need this with OR condition AND ( wp_term_relationships.term_taxonomy_id IN (2) OR tt1.term_taxonomy_id IN (11) )
Thanks in advance.
So here is my problem I want to show Posts if have same category if not have post then show post of same tag. And to do that I am using "pre_get_posts" action. and setting query like following.
function related_custom_posts($query){
$query->set( 'category__in', array(2,3) );
$query->set( 'tag__in', array(10,13) );
}
add_action( 'pre_get_posts', 'related_custom_posts', 1 );
But it create sql like this
AND ( wp_term_relationships.term_taxonomy_id IN (2) AND tt1.term_taxonomy_id IN (11) )
but i need this with OR condition AND ( wp_term_relationships.term_taxonomy_id IN (2) OR tt1.term_taxonomy_id IN (11) )
Thanks in advance.
Share Improve this question asked May 19, 2021 at 16:20 Nilesh ChouhanNilesh Chouhan 884 bronze badges1 Answer
Reset to default 1You can achieve the OR relation using the tax_query
argument like so:
$query->set( 'tax_query', array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'terms' => array( 2, 3 ),
),
array(
'taxonomy' => 'post_tag',
'terms' => array( 10, 13 ),
),
) );
本文标签: wp queryHow I can change the condition or compare operator for WPQuery in pregetposts
版权声明:本文标题:wp query - How I can change the condition or compare operator for WP_Query in pre_get_posts 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741547975a2384706.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论