admin管理员组文章数量:1122832
I was having issues when modifying the main query hooking onto pre_get_posts
as it was causing the menu to not be displayed. This the original code:
else if ( isset( $_GET['category'] ) && !is_admin() ) {
$_GET = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);
$tax_query = array(
array(
'taxonomy' => 'events-category',
'field' => 'slug',
'terms' => array( strtolower( $_GET['category'] ) ),
),
);
$query->set( 'tax_query', $tax_query );
}
Someone suggested to modify the conditional and add $query->is_main_query()
to avoid messing up the menu's query:
else if ( isset( $_GET['category'] ) && !is_admin() && $query->is_main_query() ) {
I did change it and the menus are displayed but the taxonomy query is now completely ignored and all posts are displayed.
Why would $query->is_main_query()
modify the taxonomy query? I am completely baffled.
I was having issues when modifying the main query hooking onto pre_get_posts
as it was causing the menu to not be displayed. This the original code:
else if ( isset( $_GET['category'] ) && !is_admin() ) {
$_GET = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);
$tax_query = array(
array(
'taxonomy' => 'events-category',
'field' => 'slug',
'terms' => array( strtolower( $_GET['category'] ) ),
),
);
$query->set( 'tax_query', $tax_query );
}
Someone suggested to modify the conditional and add $query->is_main_query()
to avoid messing up the menu's query:
else if ( isset( $_GET['category'] ) && !is_admin() && $query->is_main_query() ) {
I did change it and the menus are displayed but the taxonomy query is now completely ignored and all posts are displayed.
Why would $query->is_main_query()
modify the taxonomy query? I am completely baffled.
- 3 Probably because your theme is not written correctly and uses custom wp_query to display this list of posts... Bit it’s a little bit hard to guess without seeing your code... – Krzysiek Dróżdż Commented May 5, 2019 at 12:18
1 Answer
Reset to default 1In your original setup all category queries on the frontend were affected by the pre_get_posts
filter you defined. Hence its interference with the menu query. Then you added an extra condition, is_main_query
. The result being that both the menu and your main loop were no longer affected by the filter.
This can mean only one thing. What looks like the main query on your page actually is not. So the filter is not applied.
If you have written this theme yourself you could clean it up so you get the main query and the filter is applied. Otherwise, you could examine the code if there are some arguments set, which you could check with the get
method to make sure you are conditionally targeting the right query in stead of using is_main_query
.
本文标签: pre get postsquerygtismainquery() is causing query39s taxquery to be ignored
版权声明:本文标题:pre get posts - $query->is_main_query() is causing query's tax_query to be ignored 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736287114a1927810.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论