WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Interesting query question (7 posts)

  1. Doodlebee
    Member
    Posted 4 years ago #

    I have this query in place in the index page of my site:

    <?php $cats = array('0' => 'Politics',
                              '1' => 'Culture',
                              '2' => 'Campus',
                              '3' => 'Flashback'
                             );
                // earlier queries supplied other ID's to skip - this is them
                $skip = (array_merge($ids, $recentids, $scoopids));
    
                foreach($cats as $k => $v) {
    
                  $array = array('showposts' => '2',
                                 'category_name' => $v,
                                 'post__not_in' => $skip,
                                );
    
                  $my_query = new WP_Query($array);
    
    	          if($my_query->have_posts()) : ?>

    Basically I have 4 categories that I want to loop through. They all have the exact same output - grabbing the first 2 posts in each category, and laying them all out exactly the same - so it didn't make sense to me to make 4 different queries just because the category was different. So I stuck 'em in an array to Loop through and be done with it.

    It works fine, except for one thing: if a post is in multiple categories, it shows up twice in this groups of post listings. I want to be sure each post ID is unique to each category so there are no duplicates.

    Is there a way to recursively apply the already-shown post->ID so if it's in the next loop, it'll skip it and move on to the next one?

  2. MichaelH
    Member
    Posted 4 years ago #

    After if($my_query->have_posts()) : ?> couldn't you just add:

    $skip[]=(int)$my_query->post->ID; //append this post id to $skip array

    [edit]

  3. Doodlebee
    Member
    Posted 4 years ago #

    You know, Michael, I was wondering about you the other day. You are *such* a huge help, and lately I've found your posts in the forums to be incredibly helpful - I havent even needed to ask because you've got the answers posted all over the place already.

    I must buy you a prezzie. You have to have a wishlist or something somewhere, so share it :P~

    As for your solution - it *almost* works. (THANK YOU) It does skip posts that are already shown, but in the category that's skipping, it shows 1 post. I still need to to show a second post if one is skipped. That's my conundrum. (it doesn't look like it shoudl be doing that though...it looks like it should still be pulling 2 posts. So weird.)

  4. MichaelH
    Member
    Posted 4 years ago #

    Thanks for the kind words Shelly--may sound corny but that's my prezzie ;)

    As for

    I still need to to show a second post if one is skipped.

    Are you sure there are two 'undisplayed' posts left to display in that category, because your logic looks right?

  5. Doodlebee
    Member
    Posted 4 years ago #

    Yeah, I know. it totally looks like it should just add it to the "skip" query. And it is - oh wait. Oh [expletive] you're right! There's only 3 posts in this category - and the first two are already being skipped (because they are already in the "skip" array)

    Yay! Oh seriously. You are my HERO. Thank you so much!

    (I still think I should get you a prezzie.)

  6. MichaelH
    Member
    Posted 4 years ago #

    (I still think I should get you a prezzie.)

    Okay, root for the Red Sox today!

  7. Doodlebee
    Member
    Posted 4 years ago #

    Well, THAT won't be hard, since I just live about 1.5 hours from Boston! (of course, I live 1.5 hours from NYC too...) but okay - Go Red Sox! XD (I like them better anyway.)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.