1
$recent_query = new WP_Query(array (
    'post_status'   => 'publish',
    'posts_per_page' => -1
) );

//$recent_query->posts[] = get_post(1480);
//$recent_query->posts[] = get_post(1443);

array_unshift($recent_query->posts,  get_post(1480), get_post(1443));
print_r($recent_query->posts);

// The Loop
if( $recent_query->have_posts() ) : ?>
    <?php while( $recent_query->have_posts()) : $recent_query->the_post() ?>
[...]

I want to add 2 arbitrary posts to my query and loop. either of the above methods will get the posts into $recent_query->posts

Using the $recent_query->posts[] = get_post(1480); code, the manual posts will be in the array, but wont show up, in the loop.

Using array_unshift($recent_query->posts, get_post(1480), get_post(1443)); the orginal posts are deleted and shows only the manual posts, in the loop.

2 Answers 2

1

The solution is to do two queries and then a third query, to set up the wp_query:

$query1 = new WP_Query(array (
    'post_status'   => 'publish',
    'posts_per_page' => $postnumber,
    'ignore_sticky_posts' => 1,
    'cat' => $category
) );

$query2 = new WP_Query(array(
    'post_type' => 'page',
    'post__in' => array(1443,1480)));

$allTheposts = array_merge($query2->posts,$query1->posts);
foreach($allTheposts as $p){
    $ids[] = $p->ID;
}

//new query, using post__in parameter
$recent_query = new WP_Query(array(
    'post__in' => $ids,  
    'post_type' => array('page','post'), 
    'orderby' => 'post__in'
));
1
  • 1
    Im trying to do the same with one additional query, but it seems adding new posts using posts_results filter messes up the pagination and using $wp_query->set_found_posts() has no affect. So this seems a decent solution but at the cost of additional query.
    – rashid
    Commented Jul 27, 2022 at 18:41
0

Okay, so the answer to this I think is to create a new array that combines your post IDs and the specific IDs you're trying to include, probably in the same way you've done there. Then in your query args, you, you use 'post__in'=> $ids,.

Just as an example, here we had some posts selected via ACF, then ran them through our WP_Query.

    $ids = get_field( 'acf_field_name',);

    $args = [
        'post_type'      => 'posts',
        'posts_per_page' => 10,
        'post__in'       => $ids,
        'post_status'    => 'publish',
        'orderby'        => 'post__in',
    ];

All you really need to do is grab all your post ids, tack on the two in question, and use that instead of the acf get_field used in my example.

Hope that helps!

2
  • That limits the results to only the post_in array. Commented Aug 12, 2019 at 22:26
  • 1
    Right. I'm saying create yourself a new array that includes all the posts you want. ie. the post type, custom post types, or specific post ids. You can include as much or as little as you like. Then run it through there.
    – Faye
    Commented Aug 15, 2019 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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