wp_Query duplicating posts

I’m quite new to wordpress coding, although not to PHP. But I don’t quite get what is going on here. The code is:

<div class="ul-box floatRight">
            <ul>
          <?php 
wp_reset_query();   
$index_query = new WP_Query(array( 'post_type' => 'explore_more', 'posts_per_page' => 3,'order'=>'DES' ));
?>
    <?php   
    while ($index_query->have_posts()) : $index_query->the_post();  ?>
            <li class='spotlight-li'>
                <?php the_post_thumbnail(); ?>
                    <div class="explore-more-content floatLeft">
                    <h4><?php
                $title=get_the_title();
                echo showBrief($title,8);
?></h4>
                </div>
            </li>
           <?php 
    endwhile; 
        wp_reset_query(); ?>
           </ul>
        </div>

For reasons beyond me, it is producing duplicate posts. I’ve googled it multiple times, and as far as I can understand there is a double loop in there somewhere, but I don’t know where. All I can see is the while loop, unless there are more loops inside the get_the_title() function or the showBrief() function.

Read More

Can anyone help?

Related posts

Leave a Reply

1 comment

  1. Your code seem fine and should work as expected, however, showBrief() is not a native WordPress function. This is very specific to your theme, so you will need to dig around in your theme code and check what that function does exactly. Without knowing what that function does exactly, I cannot tell you if that is your problem.

    I do however have a few notes on your code:

    • 'order'=>'DES' should be 'order'=>'DESC'

    • You don’t need wp_reset_query(); before your query. The main query is already reset itself, and all custom queries should be reset

    • wp_reset_query(); after your custom query is wrong, it should be wp_reset_postdata();. wp_reset_query(); is used with query_posts which you should never use

    • Instead of using : and endwhile, rather use curlies ({}). Yes, your syntax is 100% correct, but it is hard to debug as code editors don’t support that syntax. All code editors support curlies, this makes debug a breeze