WordPress.org

Support

Support » How-To and Troubleshooting » pre_get_posts problem

pre_get_posts problem

  • I am attempting to filter a category from my homepage but when I put this code in my functions.php file, I get an error on the homepage:

    function exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
    $query->set( ‘cat’, ‘-Services’ );
    }
    }

    add_action( ‘pre_get_posts’, ‘exclude_category’ );

    Do you see a problem with this?

Viewing 11 replies - 1 through 11 (of 11 total)
  • Ok. If I replace it with this:

    function exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
    $query->set( ‘cat’, ‘-4’ );
    }
    }

    add_action( ‘pre_get_posts’, ‘exclude_category’ );

    I still get an error.

    function exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
    $query->set( 'cat', '-4' );
    }
    }

    add_action( ‘pre_get_posts’, ‘exclude_category’ );`

    Your code above works fine. By the way what error you are getting.

    To be honest, I am not sure. I am just getting an Error 500 on the homepage.

    esmi

    @esmi

    Forum Moderator

    Try checking your site’s error logs for a more specific error message.

    Ok, I figured out the error (The function was being declared twice). Now the page loads but, it is not excluding the category on the page as I had hoped.

    Are you sure “4” is the cat id for “Services”?

    I changed the code to this:

    function exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
    $query->set( ‘category_name=services’ );
    }
    }
    add_action( ‘pre_get_posts’, ‘exclude_category’ );

    And the posts still appear.

    Hi,

    I posted about this very issue here today as well:

    http://wordpress.org/support/topic/trouble-with-pre_get_posts

    There is code there that shows a solution. I don’t however know why. The code as shown in the Codex for this seems to be not working.

    The revised code is from Justin Tadlock at ThemeHybrid. He seemed to suggest he’s also been having trouble with pre_get_posts as used in the Codex example.

    function exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
    $query->set( 'category_name=services' );
    }
    }
    add_action( 'pre_get_posts', 'exclude_category' );

    Above code is not proper and there must be getting warning:
    (Warning: Missing argument 2 for WP_Query::set())

    Better to use category id for excluding such as:

    function exclude_category( $query ) {
    	if ( $query->is_home() && $query->is_main_query() ) {
    	$query->set( 'cat', '-5,-4' );
    	}
    }
    add_action( 'pre_get_posts', 'exclude_category' );

    Thank you Deepak Rajpal, your post above is working for me – I have changed is_home to is_tag for my purposes – however – it doesn’t work if I add a tag slug as well such as is_tag(‘orange’) – any suggestions?

    Many thanks 🙂

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘pre_get_posts problem’ is closed to new replies.
Skip to toolbar