WordPress.org

Forums

[resolved] Trouble with pre_get_posts (11 posts)

  1. bob.passaro
    Member
    Posted 2 years ago #

    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 ( http://codex.wordpress.org/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?

  2. Deepak Rajpal
    Member
    Posted 2 years ago #

    Just FYI, your following code works fine for me:

    function lufc_exclude_category( $query ) {
    	if ( $query->is_home() && $query->is_main_query() ) {
    		$query->set( 'cat', '-23' );
    	}
    }
    add_action( 'pre_get_posts', 'lufc_exclude_category' );
  3. bob.passaro
    Member
    Posted 2 years ago #

    Yes, it seems like it should. I was not getting any error.

    But the posts in the category 23 were still showing up. The code just didn't seem to be doing anything.

    When the function was changed as in the second piece of code, the posts in category 23 were removed.

    So, I'm not really sure what's up here.

  4. bob.passaro
    Member
    Posted 2 years ago #

    Actually the second bit of code was not working (I thought it was, but long story).

    Turns out the problem was a plugin. I am running WP e-commerce on the site. It must be using pre_get_posts and somehow overriding my function. When I disabled WP e-commerce, the altered query worked as it was supposed to.

    So for posterity, WP e-commerce apparently conflicts with using pre_get_posts to alter your main query.

  5. pepperhorn
    Member
    Posted 2 years ago #

    Hi ...

    I'm finding a similar problem with WP e-commerce ... can't seem to hook into the main_query with is_main_query through pre_get_posts (even though it should be the main query!).

    Did you manage to sort out what in WPEC was affecting the hook?

    Thanks!

    Shaun

  6. bob.passaro
    Member
    Posted 2 years ago #

    Pepperhorn,

    Yes, it appears that WP e-commerce uses pre_get_posts in a way that overrides what you try to do in functions.php or in a plug-in.

    I didn't explore that too much to figure out if there is a way to resolve it as I found a different solution for what I was trying to do.

    Maybe this is a situation to try using query_posts even though that's not the preferred method anymore.

    Or spin up a new WP_query for your blog index page. Probably not super efficient, but a workaround. Depends on what you are trying to do.

    Another solution I considered: I was just trying to exclude one category of posts. So instad of using categories, I thought about using a custom post type for that one category. I *think* custom post types are by default not included in the main query.

    So I was thinking I'd use the custom post type for those items I wanted excluded from the main query and that would happen without my having to alter the main query. Then I just make a new WP_query to call the custom post type to the page I did want them showing up on.

    If the way you were going to alter the main query was more complicated, though, that would probably be cumbersome.

    You might post about this on the WP e-commerce forum, too -- see if they have a solution.

  7. keesiemeijer
    moderator
    Posted 2 years ago #

    Have you tried it with the priority parameter.

    add_action( 'pre_get_posts', 'lufc_exclude_category', 1 );

    or

    add_action( 'pre_get_posts', 'lufc_exclude_category', 99 );

    10 being the default.

    http://codex.wordpress.org/Function_Reference/add_action

  8. bob.passaro
    Member
    Posted 2 years ago #

    Thanks, keesiemeijer.

    Worth a try. Don't know if this would break the way wp e-commerce works, but I guess there's one way to find out ...

  9. pepperhorn
    Member
    Posted 2 years ago #

    Hey guys ...

    Appreciate the input. The priority number was the next thing I was going to try. Will have a poke through the WPEC functions too .. I notice they're using pre_get_posts in a function called 'wpsc_generate_product_query' ... plus there's a variety of different priority integers assigned so it'll probably involve a bit of play to get it working right.

    Will post up any success or failure! ;) ..

    Thanks again!

    Shaun

  10. ersatzpole
    Member
    Posted 1 year ago #

    Using
    add_filter('pre_get_posts','my_query_type', 1)
    I was able to resolve the conflict with wp-ecommerce for my main query. Using the priority worked for me.

  11. bob.passaro
    Member
    Posted 1 year ago #

    ersatzpole,

    Cool. Thanks. That's good to know. I'm sure I'll run across this situation again.

Topic Closed

This topic has been closed to new replies.

About this Topic