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' );
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.
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.
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
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.
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
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 …
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
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.
ersatzpole,
Cool. Thanks. That’s good to know. I’m sure I’ll run across this situation again.