admin管理员组文章数量:1130349
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
1 Answer
Reset to default 0The 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() ) {
}
}
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
1 Answer
Reset to default 0The 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() ) {
}
}
本文标签: pre get postsWhy is pregetposts hook invoked multiple times
版权声明:本文标题:pre get posts - Why is pre_get_posts hook invoked multiple times? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749195314a2330877.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论