Support » Fixing WordPress » Only show unique articles in a triple loop

  • zonjineko

    (@zonjineko)


    I am trying to do a three category loop but I need all articles to be unique eg if one is in the first list then don’t show it in the 2nd or 3rd loop.

    Screenshot of Loop Outcome

    This comes in to play because one article could be under three different categories.

    So my code is currently as below. It generates what you see in the screenshot above. An example of what I don’t want there is the “Should I Take The JLPT” article. It should only show in the first loop but not the other two loops below it eg every article should only ever appear once across the three loops.

    <h3>TOP LEARNING ARTICLES</h3>
    
    <dl class="entryList small">
    <dt>Kanji</dt>
    <?php
    query_posts('category_name=learn-kanji&showposts=5&orderby=comment_count&order=DESC');
    $kanji_ids = array();
    while (have_posts()) : the_post();
    $kanji_ids[] = get_the_ID();
    ?>
    <dd><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></dd>
    <?
    endwhile;
    ?>
    </dl>
    
    <dl class="entryList small">
    <dt>Hiragana</dt>
    <?php
    query_posts('category_name=learnhiragana&showposts=5&orderby=comment_count&order=DESC&cat=-8');
    $hiragana_ids = array();
    while (have_posts()) : the_post();
    $hiragana_ids[] = get_the_ID();
    ?>
    <dd><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></dd>
    <?
    endwhile;
    ?>
    </dl>
    
    <dl class="entryList small">
    <dt>Grammar/Vocab</dt>
    <?php
    query_posts('category_name=vocabulary&category_name=grammar&showposts=5&orderby=comment_count&order=DESC');
    if ( have_posts() ) : while ( have_posts() ) : the_post();
    ?><dd><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></dd>
    <?php
    endwhile;endif;
    wp_reset_query();
    ?></dl>

    Thanks for any help.

Viewing 10 replies - 1 through 10 (of 10 total)
  • Michael

    (@alchymyth)

    have you really studied this:
    http://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action
    and other loop related documentation from the codex ?

    Move your query_posts parameters into an array and use the post__not_in parameter to load in the array that holds the IDs of your posts for excluding..

    Second query for example.

    $args = array(
    	'category_name' => 'learnhiragana',
    	'showposts' => 5,
    	'orderby' => 'comment_count',
    	'order' => 'DESC',
    	'cat' => -8,
    	'post__not_in' => $kanji_ids
    );
    query_posts( $args );

    … for the third query you’d do the same, but do one of two things..

    1. Join the two arrays you’ve built and feed them into post__not_in, like i’ve done above.
    2. Use one array, and feed the second queries post IDs into that array, rather then using two arrays.

    If No.1

    'post__not_in' => ( $kanji_ids + $hiragana_ids )

    Beyond that, refer to the docs as said above… 😉

    Thread Starter zonjineko

    (@zonjineko)

    alchymyth – Yep been banging my head for a week with this and tried post_not_in and a few other things I found here plus have looked at many different things I found on Google but no luck. So have done my homework before asking 🙂

    t31os_ – Thanks for your help will give that a go. I tried post_not_in and arrays but might not have got it right.

    Will try this – appreciate it.

    Thread Starter zonjineko

    (@zonjineko)

    Thx all working well except I cannot get the following to work:

    ‘post__not_in’ => ( $kanji_ids + $hiragana_ids )

    Is the + sign meant to be there? It doesn’t work for me. I have ooked and looked and cannot see how to have two items in the posts__not_in line.

    <dl class="entryList small">
    <dt>Grammar/Vocab</dt>
    <?php
    $args = array(
        'cat' => '134,123',
        'showposts' => 5,
        'orderby' => 'comment_count',
        'order' => 'DESC',
        'post__not_in' => ($hiragana_ids)
    );
    // print_r(array($args));
    query_posts( $args );
    $vocabgrammar_ids = array();
    while (have_posts()) : the_post();
    $vocabgrammar_ids[] = get_the_ID();
    ?>
    <dd><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></dd>
    <?
    endwhile;
    ?>
    </dl>

    It’s just an easy way for joining 2 arrays…

    If you use 1 array you won’t need to do this, use the same array for each of the loops.

    $kanji_ids[] = get_the_ID();

    Thread Starter zonjineko

    (@zonjineko)

    Thx but I’m using three arrays – this is just the final one that I showed above.

    I’m really still just trying to get the final loop of the three to not show what is in the first two loops. I can’t seem to get it working at all. I can get it to not show one or the other but not the two.

    Not sure what you mean by use the same array for each of the loops.

    Starting to feel very dumb 🙂

    // Create an empty array (do this before any queries)
    $some_array = array();
    
    // Inside first loop
    $some_array[] = get_the_ID();
    
    // In second query args
    'post__not_in' => $some_array
    
    // Inside the second loop
    $some_array[] = get_the_ID();
    
    // In third query args
    'post__not_in' => $some_array
    
    // Inside the third loop the array will be filled with post ids from both the first two loops.

    Does that help?

    Thread Starter zonjineko

    (@zonjineko)

    Ok cool – think that has finally sunk into my thick head 🙂

    Will try that now – thx for the extra help.

    Thread Starter zonjineko

    (@zonjineko)

    Okay that works perfectly – thanks again for your perseverance with helping me. All looks simple now that I know how 🙂

    No problem, post back if you have a problem understanding… 😉

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Only show unique articles in a triple loop’ is closed to new replies.