Trouble with pre_get_posts
-
I’m trying to use pre_get_posts to exclude a category from the regular blog index — pretty much just like the example in the Codex ( https://codex.www.ads-software.com/Plugin_API/Action_Reference/pre_get_posts#Exclude_categories_on_your_main_page )
I have the following in functions.php:
It’s a child theme, so inside the child theme setup function is:
add_action( 'pre_get_posts', 'lufc_exclude_category' );
Then the function comes later, outside the setup function :
function lufc_exclude_category( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'cat', '-23' ); } }
I’ve checked and rechecked the category ID number, and everything else. Not working.
I got some help on another forum to end up getting this working using the following more complicated code:
function lufc_exclude_category( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'tax_query', array( array( 'taxonomy' => 'category', 'field' => 'id', 'terms' => array( 23 ), 'operator' => 'NOT IN' ) ) ); } }
My question is why wasn’t the earlier code — more or less straight out of the Codex — working?
- The topic ‘Trouble with pre_get_posts’ is closed to new replies.