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.

Share Improve this question edited May 5, 2019 at 12:20 cjbj 15k16 gold badges42 silver badges89 bronze badges asked May 5, 2019 at 12:05 csaboriocsaborio 1122 silver badges13 bronze badges 1
  • 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
Add a comment  | 

1 Answer 1

Reset to default 1

In 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