0

I have a WP_Query with rewind_posts that I use to filter posts if they belong to a particular category. Basically, if any post belongs in a category with First, it goes to the top of the list.

The loop does that, the only problem is that it creates duplicate posts. The post that's in the First category is at the top and it's also with the rest of the group.

Example of my problem:

  • Title - First
  • Title 2
  • Title 3
  • Title - First
  • Title 4
  • Title 5

Any ideas on how to fix this? This is the code I'm using for the loop:

<?php $args = array(
'tax_query' => array(
array(
    'taxonomy' => 'post-status',
    'field' => 'slug',
    'terms' => array ('post-status-published')
    )
)
); $query = new WP_Query( $args );?>
<div class="container">
<?php if ( have_posts() ) {
while( $query->have_posts() ) {
    $query->the_post(); ?>

    <?php if (in_category('First') ) { ?>
        <?php the_title();?><br />
    <?php }

}  // end first while loop 

rewind_posts(); // rewind loop so we can rerun it ?>

<?php while( $query->have_posts() ) { // Start second while loop
    $query->the_post(); ?>

     <?php if (in_category('Second') ) { ?>
        <?php the_title();?><br />
    <?php }

}  // end first while loop 

rewind_posts(); // rewind loop so we can rerun it ?>

<?php while( $query->have_posts() ) { // Start second while loop
    $query->the_post(); ?>

    <?php the_title();?><br />

<?php } // end second while loop

rewind_posts(); // rewind loop so we can rerun it

} // End your if statement ?>

Thanks for the help.

3

You have a couple of issues here

  • You are checking whether the main query returned any posts and then loop through the posts of your custom query

  • rewind_posts() is wrongly used. By default, rewind_posts() rewinds the loop of the main query. You should rewind the posts of your custom query.

  • Your last loop just basically loops through all the posts again and then displays them all

  • If a post belongs to more than one term, you can have duplicates. Best to do a check to avoid those

  • Your tax_query does not really make sense. In your loop you are checking terms from the default taxonomy category while you are querying posts from another custom taxonomy. Anyways, that is for you to think about

Lets look at a skeletal solution

$args = [
    // Your query parameters
];
$query = new WP_Query( $args );

// Test to see if you have posts from the custom query, not main query
if ( $query->have_posts() ) :
    // Create an array to avoid duplicates
    $duplicates = [];

    // Run the loop for the first time
    while ( $query->have_posts() ) :
        $query->the_post();

        if ( in_category( 'First' ) ) :
            // Display what you need
            the_title();

            // Save the post ID to avoid duplicates
            $duplicates[] = get_the_ID();

        endif;
    endwhile; // End your first loop

    $query->rewind_posts(); // Rewind the custom loop

    // Start the second loop
    while ( $query->have_posts() ) :
        $query->the_post();

            if ( in_category( 'Second' ) ) :
                // Make sure the post is not duplicated
                if ( in_array( get_the_ID(), $duplicates ) )
                    continue;

            // Post is not duplicate, display it
            the_title();

            // Save the post ID to $duplicates
            $duplicates[] = get_the_ID();

        endif;
    endwhile; // End second loop

    $query->rewind_posts(); // Rewind second custom loop          

    // Run your loop one more time
    while ( $query->have_posts() ) :
        $query->the_post();

            // Check if the post is already displayed, if so, skip it
            if ( in_array( get_the_ID(), $duplicates ) )
                continue;

            // Display the post
            the_title();

    endwhile; // Close the last loop
    wp_reset_postdata(); // Reset $post global
endif; 
  • Thanks for the help. The taxonomy is needed because I'm creating a page that changes based on the date and what posts are created in a date range. The code above gave me a white screen. – Gregory Schultz Jul 11 '16 at 7:21
  • What errors are you getting, what PHP version are you using. I'm using short array syntax that is only available since PHP 5.4. Also note, as I stated, my code is just a skeleton and therefor not a copy and paste correct example – Pieter Goosen Jul 11 '16 at 7:24
  • Unexpected if on if ( $query->have_posts() ) : – Gregory Schultz Jul 11 '16 at 7:34
  • Code updated. I missed a ;. I'm coding from a mobile device so expect some syntax errors. – Pieter Goosen Jul 11 '16 at 7:37
  • Just found the error as well. Thanks for the help! You rock! – Gregory Schultz Jul 11 '16 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.