Support » Fixing WordPress » Multiple Loops and Duplicate Issues

  • Resolved yekxmerr

    (@yekxmerr)


    WP_Query:

    <?php $popular_loop = new WP_Query( array('v_sortby' => 'views', 'posts_per_page' => '4'));
          if( $popular_loop->have_posts() ): while( $popular_loop->have_posts() ): $popular_loop->the_post(); $do_not_duplicate[] = $post->ID ?>
          <li>
          <a href="<?php the_permalink() ?>" target="_blank" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a>
          <h3><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" target="_blank"><?php $thetitle = $post->post_title;$getlength = mb_strlen($thetitle);$thelength = 19;echo substr($thetitle, 0, $thelength);if ($getlength > $thelength) echo "..."; ?></a></h3>
          <p><?php echo get_post_meta($post->ID,'Resumo',true); ?></p>
          </li>
          <?php endwhile; endif; wp_reset_postdata(); ?>

    Main Loop:

    <?php if (have_posts()) : while (have_posts()) : the_post(); if (in_array($post->ID, $do_not_duplicate)) continue; ?>
          <li><a href="<?php the_permalink() ?>" target="_blank" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a>
          <h3><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" target="_blank"><?php $thetitle = $post->post_title;$getlength = mb_strlen($thetitle);$thelength = 19;echo substr($thetitle, 0, $thelength);if ($getlength > $thelength) echo "..."; ?></a></h3>
          <p><?php echo get_post_meta($post->ID,'Resumo',true); ?></p></li>
          <?php endwhile; endif ?>

    What i want is to show the main loop with 20 posts (i’ve set in general settings to show only 20 posts) and a second wp_query with 4 posts. The main loop needs to exclude the posts from the wp_query and it’s working. The only problem i have here is that the main loop is missing 1 post. It only shows 19 posts instead of 20. This website only have 24 posts for testing purposes. Any ideas why it shows minus 1 post ?

Viewing 6 replies - 1 through 6 (of 6 total)
  • yekxmerr

    (@yekxmerr)

    It works if i use the query_posts on main loop:

    <?php query_posts(array('post__not_in'=>$do_not_duplicate));
     if (have_posts()) : while (have_posts()) : the_post();
     ?>

    Can’t understand why my way didn’t work.

    Michael

    (@alchymyth)

    Forum Moderator

    http://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action

    can you post the full code of the template, assuming that both loops are in one template?

    I don’t see any coding where you actually collect the ‘duplicate’ post ids…

    yekxmerr

    (@yekxmerr)

    Here you go, i solved it with query_posts but the archive.php is similar to the index file so the problem will persist.

    http://pastebin.com/Kvk2k8gD

    yekxmerr

    (@yekxmerr)

    The thing is $popular_loop displays the most viewed posts and the main loop displays the normal posts, without the most viewed ones from the $popular_loop. The code above is updated to query_posts, but it’s the same thing.

    Michael

    (@alchymyth)

    Forum Moderator

    i solved it with query_posts

    try to merge the ‘post__not_in’ with the query;
    http://codex.wordpress.org/Function_Reference/query_posts#Preserving_Existing_Query_Parameters

    i.e. look at line 26 from your pastebin:

    <?php query_posts(array('post__not_in'=>$do_not_duplicate)); if (have_posts()) : while (have_posts()) : the_post(); ?>

    and try to change it to:

    <?php global $wp_query;
    $args = array_merge( $wp_query->query_vars, array( 'post__not_in' => $do_not_duplicate) );
    query_posts( $args );
    if (have_posts()) : while (have_posts()) : the_post(); ?>

    that should allow the code to work in archive pages as well.

    untested

    yekxmerr

    (@yekxmerr)

    Alchymyth you’re amazing!
    It work, and it seems not to affect the pagination.
    Thank you for helping me out.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Multiple Loops and Duplicate Issues’ is closed to new replies.