$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>pre get posts - Why is pre_get_posts hook invoked multiple times?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

pre get posts - Why is pre_get_posts hook invoked multiple times?

matteradmin10PV0评论

I am using pre_get_posts hook, however as it is not working as expected, I have discovered that the function is being invoked 4 times, and while the first call is running as expected, subsequent calls will not run correctly. I wonder why my custom hook is being run more than once!

I have found an article that explains why this may happen, but I am confused as in my case this shouldn't happen at all....

Code:

function exclude_category( $query ) {
    global $acorn_user;
    $tax_query = array();

    if ( $query->is_feed ) {
        /**** Exclude "exclude" category posts from feed ********/
        $query->set('cat', '-617, -618');
        return $query;
    }

    $args = array(
        'category__not_in' => 2 ,
        'category__in' =>  22,
        'posts_per_page' => 7,
        'post_status' => 'publish');

    if (!is_user_logged_in()  /*&& !is_admin()*/) {
        /**** Exclude child categories ********/
        if ($query->is_category(3) || $query->is_category(68) || $query->is_category(69) || $query->is_category(70)) {
            $queried_object = get_queried_object();
            $child_cats = (array)get_term_children($queried_object->term_id, 'category');

            if (!$query->is_admin) {
                //exclude the posts in child categories
                $tax_query[] = array('category__not_in', array(68, 69, 70, 81, 82, 83));
            }
        }
    }
    //exclude search results for parents & teachers & non logged users
    if ( $query->is_search && $query->is_main_query() ) {
        if(empty($acorn_user) || !in_array( 'teacher', (array) $acorn_user->roles )) {
            $tax_query[] = array('post__not_in', array(30140, 30020, 30008, 29998, 29991, 21458,20197,11986,6614));
        }
    }
    if (!empty($tax_query)) {
        $query->set('tax_query', $tax_query);
    }
    return $query;
}
add_filter( 'pre_get_posts', 'exclude_category' ); 

I am using pre_get_posts hook, however as it is not working as expected, I have discovered that the function is being invoked 4 times, and while the first call is running as expected, subsequent calls will not run correctly. I wonder why my custom hook is being run more than once!

I have found an article that explains why this may happen, but I am confused as in my case this shouldn't happen at all....

Code:

function exclude_category( $query ) {
    global $acorn_user;
    $tax_query = array();

    if ( $query->is_feed ) {
        /**** Exclude "exclude" category posts from feed ********/
        $query->set('cat', '-617, -618');
        return $query;
    }

    $args = array(
        'category__not_in' => 2 ,
        'category__in' =>  22,
        'posts_per_page' => 7,
        'post_status' => 'publish');

    if (!is_user_logged_in()  /*&& !is_admin()*/) {
        /**** Exclude child categories ********/
        if ($query->is_category(3) || $query->is_category(68) || $query->is_category(69) || $query->is_category(70)) {
            $queried_object = get_queried_object();
            $child_cats = (array)get_term_children($queried_object->term_id, 'category');

            if (!$query->is_admin) {
                //exclude the posts in child categories
                $tax_query[] = array('category__not_in', array(68, 69, 70, 81, 82, 83));
            }
        }
    }
    //exclude search results for parents & teachers & non logged users
    if ( $query->is_search && $query->is_main_query() ) {
        if(empty($acorn_user) || !in_array( 'teacher', (array) $acorn_user->roles )) {
            $tax_query[] = array('post__not_in', array(30140, 30020, 30008, 29998, 29991, 21458,20197,11986,6614));
        }
    }
    if (!empty($tax_query)) {
        $query->set('tax_query', $tax_query);
    }
    return $query;
}
add_filter( 'pre_get_posts', 'exclude_category' ); 
Share Improve this question asked Nov 9, 2018 at 10:28 RiccardoRiccardo 9711 gold badge18 silver badges37 bronze badges 2
  • pre_get_posts runs any time posts are queried. This includes the main list of posts as well as menus or widgets. If you only want to affect the main posts list you need to check $query->is_main_query() – Jacob Peattie Commented Nov 9, 2018 at 10:46
  • Thanks Jacob, I'd use this as an accepted solution! – Riccardo Commented Nov 9, 2018 at 11:28
Add a comment  | 

1 Answer 1

Reset to default 0

The pre_get_posts action runs any time posts are queried. This obviously includes the main query for displaying your latest posts or the current page, but it also includes any other time posts are queried. So that would include any use of WP_Query() or get_posts() in a plugin or your templates, any Recent Posts or similar widgets, and menus (menu items are actually a post type, so any menu on your site will involve a posts query).

If you only want to affect the main query (that being the one you loop over with have_posts(), you need to check $query->is_main_query():

function wpse_318765_pre_get_posts_callback( $query ) {
    if ( $query->is_main_query() ) {

    }
}
Post a comment

comment list (0)

  1. No comments so far