1

I trying exclude category "1" with pre_get_posts:

function exclude_category( $query ) {
    if( !is_admin() && $query->is_home() && $query->is_front_page() && $query->is_main_query() && $query->is_tag() && $query->is_search())
    {
        $query->set( 'cat', '-1' ); // I also trying without the '' on the -1 (cat id)
    }
}
add_action( 'pre_get_posts', 'exclude_category' );

I'am use on my pages the main query:

if (have_posts()) : while (have_posts()) : the_post();
  • Try adding wp_die('hello'); in the IF statement to see if it even hits. – Liam Stewart Jun 14 '18 at 17:28
2

There are a couple of problems with your condition:

A single query cannot match all of your conditions - it can't be a tag as well as search, for example. You need to use || (or) and possibly nest - so for example,

if(!is_admin() && ($query->is_home() || $query->is_search())) {

This says, "if we're not in the admin area AND either it's a home query or a search query, then do this."

Also, some of the is_ conditions you're trying to meet aren't the way you test the query. is_front_page() cannot be called because the query isn't set up yet - but is_home() can. Read up in the documentation to learn more about which conditionals are valid, and then you can try a simplified query first - such as if(!is_admin()) to make sure it's running - then add additional conditions one at a time so you can tell which one specifically is causing you problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.