• I’m using the “pre_get_posts” hook to modify the WordPress query for 2 specific categories on a site. Everything is working fine until it gets to this point:

    if($query->is_main_query()) {
    	$query->query_vars['post__in'] = $pageposts_arr;
    	$query->query_vars['orderby'] = 'post__in';
    }

    $pageposts_arr is just an array of post IDs in a specific order determined by a special mysql query. I’ve tried both $query->is_main_query() and is_main_query(), neither return any value. Everything works fine if I remove the if test, except that it breaks all the other queries on the page. I can confirm that it was working fine in 3.7, and broke immediately upon upgrading to 3.8.

    Been searching for quite a while, but noone else seems to be having this problem. Anyone know what’s up?

Viewing 5 replies - 1 through 5 (of 5 total)
  • 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.www.ads-software.com/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! ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Upgrade to 3.8 breaking is_main_query() function’ is closed to new replies.