• Resolved bigflannel

    (@bigflannel)


    I am trying to understand why the Query Block has stopped working with a custom theme in WP6.2.

    I have a fairly basic custom theme I keep up-to-date and replicate to develop with. The Query Block works with this theme in WP6.1.1 but any page with a Query Block in it in WP6.2 returns no posts. There are no php errors, wordpress errors, and any blocks preceeding the Query Block are not returned either. This is true when inheriting the query, or when setting the query. There are no plugins installed. The Query Block works with the default theme in WP6.2. My default theme is not a block theme. I don’t think Twenty Twenty-One is either. The Query Block works with Twenty Twenty-One.

    I’ve reviewed my functions.php file for anything that re-writes queries.

    I am at a loss for what to consider next. I can’t understand why there are no posts found rather than an error. I can’t think where my theme impacts the query return (other than in functions.php).

    Any suggestions on where to focus would be gratefully received.

    Regards
    Mike

    • This topic was modified 1 year, 6 months ago by bigflannel.
Viewing 4 replies - 16 through 19 (of 19 total)
  • Hi – sorry for response delay – was out of town. I’m using a custom child theme of Understrap. My homepage template used:

    <?php while ( have_posts() ) { 
    the_post();
    get_template_part( 'loop-templates/content', 'home' );
    }
    ?>

    I changed this to use only

    <?php get_template_part( 'loop-templates/content', 'home' );
    ?>

    However, why the first worked in 6.1.2 and not in 6.1.3 is a mystery.

    Moderator bcworkz

    (@bcworkz)

    Hello Mike (and swcomm),
    Sorry for the long delay before responding. By the time I got home I’d forgotten about this topic. Combined with forum/slack notifications not reliably working for me, you essentially “fell through the cracks”. I appreciate your patience.

    It seems swcomm has confirmed Mike’s suspicion about have_posts() being the cause despite my skepticism. However, it’s really just another clue, it couldn’t be the root cause. All it really does is return true when the query object’s props evaluate as current_post+1 < post_count.

    How would the query loop mess this up? It could since it runs it’s own WP_Query object. For some reason it’s apparently walking on your query object’s props. It sounds like the query loop might be neglecting to call wp_reset_postdata() so that your own loop thinks it has completed because the query loop block’s loop had completed.

    But that cannot be the case because it does not affect all themes, you two are the only ones I’ve heard of. Not that I read all forum topics, there likely are others. Every WP classic theme in existence uses have_posts() though, only a select few are encountering this problem.

    I cannot conceive how your theme loops would be influenced by query loop blocks, but a possible workaround would be for your templates to immediately copy the global $wp_query object into another var and run your loops off of that var using its own methods instead of the global ones. For example:

    /* Template header comment */
    global $wp_query;
    $the_query = $wp_query;
    
    //typical template code prior to loop here
    
    if ( $the_query->have_posts() ) {
            $the_query->the_post();
            // you usual loop code here
    } else {
    	esc_html_e( 'Sorry, no posts matched your criteria.' );
    }
    // the rest of your template code here

    I’m not sure if $the_query is within scope of template parts. I think so, but if not you may want to make $the_query global in scope. But $the_query is certainly out of scope to query loop blocks, so they cannot influence $the_query count props.

    Somewhat a Band-Aid solution since it doesn’t address the root cause. The root cause would something about how your theme uses its query objects, but I’ve no idea what that might be.

    Thread Starter bigflannel

    (@bigflannel)

    @bcworkz – thank you! I don’t mind waiting all year and truly appreciate your help.

    I have access to all the template code, I wrote the whole theme. I replaced the query in single.php with the code you suggest and it works. The single with a query block in it renders the query block. Thank you!

    Of course there is part of me thinking why! So far, I have just checked the workaround works, but in the back of my mind, the Classic Block is calling. I made all posts that contain the Classic Block in my development wordpress install draft. The last (most recent) post in my development wordpress install contains a query block. The index did not display the sticky post after the last post with the query block before making all posts with classic blocks draft, but did after. I am also using a custom plugin in the Classic Block, which I have not looked at yet. I am not sure if the Classic Block is anything to do with this just yet, but it seems to be something. I will keep posting anything I learn. @swcomm do you have classic blocks in any of your post content?

    Once again, thank you @bcworkz.

    Regards
    Mike

    Hi – no, I’m not using the Classic block in any of the loops. When I first started troubleshooting this, my initial thought is that it had something to do with sticky posts, as my first two blocks (which always rendered just fine) make extensive use of sticky+categories+offsets, and the third block that stopped working in 6.1.3 excludes sticky posts completely. That wasn’t the case tho.

Viewing 4 replies - 16 through 19 (of 19 total)
  • The topic ‘Query Block works in WP6.1.1 but not WP6.2 with my custom theme’ is closed to new replies.