PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 30, 2022

[FIXED] WP_Query sometimes returns zero results when there are matching posts - why?

 January 30, 2022     wordpress     No comments   

Issue

This one has got me stumped.

I have a category.php file which contains this loop:

<?php
if ( have_posts() ) : ?>
    <?php
    while ( have_posts() ) : the_post(); ?>
        <div class="entry-content description clearfix">
            <h2 class="category-subtitle"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <?php echo the_content(); ?>
            <?php global $withcomments; $withcomments = 1;
    ?>
        </div><!-- .entry-content -->
    <?php
    endwhile;
else :
    get_template_part( 'content', 'none' );
endif;
?>

This block of code works fine and always returns the results one would expect.

In addition, outside the loop (after it, in case that matters), I've got a column to one side with this loop - I'm going to refer to this as the newsfeed loop for the sake of clarity:

<h3 class="newsfeed-heading"><a href="/category/news/">Latest News</a></h3>
<?php
    // wp_reset_query(); * Same results with or without wp_reset_query
    $args = array(
    'cat' => 89,
    'order'   => 'ASC'
    );
    $custom_query = new WP_Query($args);
    //echo "<h2>Found: $custom_query->found_posts</h2>";
    while($custom_query->have_posts()) : $custom_query->the_post();
?>
    <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
        <h4 class="highlight1"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
        <p><?php $content = get_the_content(); echo mb_strimwidth($content, 0, 160, '...');?></p><div class="morelink"><a title="<?php the_title(); ?>" href="<?php the_permalink(); ?>">Read more</a></div>
    </div>
<?php endwhile; 
  // wp_reset_postdata();  * Same results with or without wp_reset_postdata
?>

Now, in the majority of cases, these two loops play nicely together and both loops return the set of results I'd expect. But not always.

As far as I can tell, I think if the main category loop only contains one result, then the newsfeed loop is always correct. But if the main category loop has multiple posts in the results set, the newsfeed loop frequently works fine. So I have not been able to spot a pattern.

I have tried putting wp_reset_query(); in different places, clutching at straws, but it doesn't make any difference.

Incidentally, when the newsfeed loop works, it always returns the correct results set. But when it doesn't, it always returns nothing and $custom_query->found_posts returns zero.

I would really appreciate some advice on where to look for a possible solution.


Solution

Solved!

I was convinced that the problem wasn't in the code and I should have worked this out sooner.

The problem was caused by a Wordpress plugin called ReOrder Post Within Categories https://wordpress.org/plugins/reorder-post-within-categories/

This plugin allows you to manually reorder selected Categories via a drag-and-drop interface in the Dashboard.

This explains why the custom query worked most of the time - only about ten of the 80+ Categories was being manually re-ordered and of course those are the ones that cause the conflict with my WP_Query.

I'm not in a position to start hacking at the plugin so the workaround is to disable the plugin and reorder posts by altering the publication dates instead.

Thank you to the contributors who attempted to answer this question for me.



Answered By - Jon Ewing
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing