Thread Starter
ajc458
(@ajc458)
I’ve narrowed it down to the is_category() function i’m calling before is_main_query(). It seems to make is_main_query() return no value.
if(is_category(14)) {
if($query->is_main_query()) {
$query->query_vars['post__in'] = $pageposts_arr;
$query->query_vars['orderby'] = 'post__in';
}
}
Edit: It seems that the reverse is broken as well. Whichever function call is first works fine.
Edit: I found a temporary solution. Change the
is_category()
to
$query->query[‘category_name’] == ‘the-category-slug’
Replacing “the-category-slug” with the actual slug.
Thank you for the solution, I was experiencing the same problem. Looks like this should be fixed in 3.8.1: https://core.trac.wordpress.org/ticket/26627
Thread Starter
ajc458
(@ajc458)
Awesome, glad I could help. I didn’t think to submit it as a bug report.
Hello,
I think I’m having the same (or a similar) problem as above, however I am running WordPress 3.8.1.
I am testing with the following pre_get_posts hook:
add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
echo 'is_main_query: ' . ($query->is_main_query() ? 'y' : 'n');
}
With an unmodified twentyfourteen theme this results in 4 prints of “is_main_query” where only the first one prints is_main_query: y while the other 3 prints is_main_query: n
When the code gets to the “main div” (where WP prints a list of posts) is_main_query is false.
The first two calls to is_main_query happens even before any HTML is output.
However, looking at the implementation of WP_Query::is_main_query() I am not sure my results say anything substantial… and looking at that code it seems to me any kind of state cannot be changed (only the identity of the WP_Query object being used).
I am trying to implement listing of custom post types with the following (well known) implementation of the “pre_get_posts” hook:
function my_get_posts( $query ) {
if ( $query->is_home() && $query->is_main_query() )
$query->set( 'post_type',
array( 'post', 'page', 'character', 'location' ) );
return $query;
}
As you can imagine, this does not give the desired result… because is_main_query is false where the posts should be printed.
Am I supposed to use some other kind of test than is_main_query? Or is this another bug? The same bug?
Kind Regards,
/Erik
Hmmm, it seems I have to apologize. My problem doesn’t seem to have anything to do with this problem after all.
If I just do:
function my_get_posts( $query ) {
$query->set( 'post_type',
array( 'post', 'page', 'character', 'location' ) );
}
…I can make posts and pages appear on the first page, but still not “characters” and “locations”… seems there’s something wrong with something else…
Oh, yeah…I’m a blind fool! It’s registered as:
register_post_type( 'character_post_type', $args );
Using this code:
function my_get_posts( $query ) {
$query->set( 'post_type',
array( 'post', 'page', 'character_post_type', 'location_post_type' ) );
}
…Gives me the desired results…
Phew! I’m sorry! 😐