WordPress.org

Ready to get started?Download WordPress

Forums

pre_get_posts problem (12 posts)

  1. prologuemedia
    Member
    Posted 1 year ago #

    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?

  2. alchymyth
    Forum Moderator
    Posted 1 year ago #

  3. prologuemedia
    Member
    Posted 1 year ago #

    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.

  4. Deepak Rajpal
    Member
    Posted 1 year ago #

    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.

  5. prologuemedia
    Member
    Posted 1 year ago #

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

  6. esmi
    Forum Moderator
    Posted 1 year ago #

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

  7. prologuemedia
    Member
    Posted 1 year ago #

    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.

  8. Deepak Rajpal
    Member
    Posted 1 year ago #

    Are you sure "4" is the cat id for "Services"?

  9. prologuemedia
    Member
    Posted 1 year ago #

    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.

  10. bob.passaro
    Member
    Posted 1 year ago #

    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.

  11. Deepak Rajpal
    Member
    Posted 1 year ago #

    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' );
  12. PinkishHue
    Member
    Posted 7 months ago #

    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 :)

Topic Closed

This topic has been closed to new replies.

About this Topic