• Resolved sheriffderek

    (@sheriffderek)


    I’m looping through an array of post ids with wp query and post__in.

    I have some generic posts that I reuse (so, reusing their id) – but it appears to be stripping out the duplicates. All the questions I can find area about NOT having duplicates, but I want them! : )

    
    $lessons = [
          '2953', // lesson name etc.
          '2963',
          $sunday, // reused id of '4321
    
          $practice, // a reused id of '1234'
          $practice, //
          $practice, //
    ];
    

    This sort of thing.

    Any leads on how I can keep these / or keywords to look for!?

    : )

    • This topic was modified 3 years, 1 month ago by sheriffderek.
    • This topic was modified 3 years, 1 month ago by sheriffderek.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter sheriffderek

    (@sheriffderek)

    It seems to take the $vacation, $sunday, and $practics “lesson: posts – and put them in only once… and at the end of the list.

    Moderator bcworkz

    (@bcworkz)

    The way WP_Query uses “post__in” query var makes it unable to get duplicate posts as you want. I think you’d need to loop through your source data array to get individual posts with get_post()

    The reason WP_Query cannot work is “post_in” gets translated into SQL something like this:
    SELECT * FROM wp_posts WHERE ID IN (2953, 2963, 4321, 1234, 1234)
    No matter how many times 1234 occurs, the DB engine sees it as one match in the array.

    Thread Starter sheriffderek

    (@sheriffderek)

    Gotcha! Thanks. I thought there might be a flag “show__duplicates” or something smooth. : )

    I went with this:

    
    global $post;
     foreach ($customLessonPlanIdArray as $id) {
       $post = get_post($id);
       setup_postdata( $post );
    } 
    

    But I didn’t feel very in control of it. In my case – there’s only one type of post on the page, but I’m not familiar with the global post object. It seems like a classic copy-paste job! But it works. : )

    • This reply was modified 3 years, 1 month ago by sheriffderek.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Allow repeated posts (based on id) with post__in and wp_wquery’ is closed to new replies.