最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

archives - Modify query in pre_get_posts action is messing up my nav menu

programmeradmin0浏览0评论

I am modifying the query for custom archive pages. I am using the pre_get_posts action. I need to catch some GET variables to modify my query. It is working great on the archive page for one custom post type but not the rest. It is breaking my navigation menus.

add_action( 'pre_get_posts', 'search_provider');
function search_provider($query){
if(is_post_type_archive(array('provider'))){
   $query->set( 'posts_per_page', -1 );
   $query->set( 'orderby', 'title' );
    $query->set( 'order', 'ASC' );
    if(isset($_GET['providersearch']) && $_GET['providersearch'] == 'Y'){
        if(!empty($_GET['s']))
        $query->set( 's', $_GET['s'] );
    } else {
        $query->set ('exclude', array(10054, 10068));
    }
  }
}

Do I need to clear the query variables after the content is returned?

I am modifying the query for custom archive pages. I am using the pre_get_posts action. I need to catch some GET variables to modify my query. It is working great on the archive page for one custom post type but not the rest. It is breaking my navigation menus.

add_action( 'pre_get_posts', 'search_provider');
function search_provider($query){
if(is_post_type_archive(array('provider'))){
   $query->set( 'posts_per_page', -1 );
   $query->set( 'orderby', 'title' );
    $query->set( 'order', 'ASC' );
    if(isset($_GET['providersearch']) && $_GET['providersearch'] == 'Y'){
        if(!empty($_GET['s']))
        $query->set( 's', $_GET['s'] );
    } else {
        $query->set ('exclude', array(10054, 10068));
    }
  }
}

Do I need to clear the query variables after the content is returned?

Share Improve this question edited May 1, 2018 at 11:34 Peter Mortensen 2682 silver badges10 bronze badges asked May 1, 2018 at 5:09 jppower175jppower175 2832 silver badges13 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 8

pre_get_posts runs for every query. That includes the main query, secondary queries, and menus. The problem is that is_post_type_archive(array('provider')) is checking if the main query is the post type archive, not a specific query going through pre_get_posts.

So when you check for that and then modify the current query going through pre_get_posts you're going to modify all queries based on a condition of the main query.

To do what you want properly you need to check each query in pre_get_posts to check that the particular query that is being passed to the action callback is for the post type archive. In other words the same query that you are setting the arguments on with $query->set().

You can do that by using the $query->is_post_type_archive() method.

add_action( 'pre_get_posts', 'search_provider'); 
function search_provider( $query ) {
    if ( $query->is_post_type_archive( array( 'provider' ) ) ) {
        $query->set( 'posts_per_page', -1 );
        $query->set( 'orderby', 'title' );
        $query->set( 'order', 'ASC' );

        if ( isset( $_GET['providersearch'] ) && $_GET['providersearch'] == 'Y' ) {
            if ( ! empty( $_GET['s'] ) ) {
                $query->set( 's', $_GET['s'] );
            }
        } else {
            $query->set( 'exclude', array( 10054, 10068 ) );
        }
    }
}

Now $query->set() will only set values if that specific query is a post type archive.

should check this first :

  if ($query->get('post_type') == 'nav_menu_item')
    return $query;

like here :

    function wp32151_search_filter($query)
{
    if ($query->get('post_type') == 'nav_menu_item')
        return $query;

    if ($query->is_search) {
        $query->set('post_type', 'shows');
    } 
     return $query;
}

add_filter('pre_get_posts', 'wp32151_search_filter');
发布评论

评论列表(0)

  1. 暂无评论