I'm attempting to do this but with multiple posts and specific positions. The post positions are hard coded right now, but will be pulled in from post meta. This code is only being ran on the homepage and the posts aren't paginated.

$essay_args = array(
    'posts_per_page' => get_option('posts_per_page'),
    'post__not_in' => array($post->ID),
    'post_type' => array('post')
$essays = get_posts( $essay_args );

$fixed_posts = get_posts(array('post_type'=>array('ad','book')));

$positions = array(2,5,10); // to be pulled in from ad/book posts
$x = 0;

foreach($fixed_posts as $fixed_post) : setup_postdata($post);
    $e_posts = new WP_Query(array('p'=>$fixed_post->ID,'post_type'=>array('ad','book')));
    if (!empty($e_posts->posts)) array_splice($essays,$positions[$x],0,$e_posts->posts);

 foreach($essays as $essay) : setup_postdata($post);
    echo $essay->post_title . '<br>';

Does it make more sense to do multiple WP_Query/get_posts requests for each post by id or does that tax the database too much? Is a there a cleaner way to do this?

  • You already have set of posts in $fixed_posts, why are you querying them one by one again with WP_Query?
    – Rarst
    Commented Mar 17, 2014 at 19:46
  • I was using that to grab the correct id's. Are you suggesting I can use the id's already stored in $fixed_posts ? Commented Mar 17, 2014 at 21:30
  • Not sure I am quite following this but essentially it looks like "query me a post for this ID of a post I already have". Why? That whole foreach is tad weird overall.
    – Rarst
    Commented Mar 17, 2014 at 21:34
  • That's what I was doing, but I can see that I can just splice the posts in with the id's I already have. Commented Mar 17, 2014 at 21:52

1 Answer 1


You can try rebuilding an array of the two queries or splice the fixed_posts into the essays array. Maybe something like below.

$essay = get_posts();
$fixed_posts = get_posts();
$post_positions = array(2,5,10);
$x = 0;

foreach ($essays as $i => $essay) {
    if ($post_positions[$x] == $i) {
        $top_posts[] = $fixed_posts[$x];

    $top_posts[] = $essay;

// Alternatively
foreach ($fixed_posts as $i => $fixed_post) {
    array_splice($essays, $post_positions[$i], 0, $fixed_post);
  • You seem to be missing a continue; in your foreach-loop inside the if statement. In the current setup you will be setting your values twice.
    – kaiser
    Commented Mar 19, 2014 at 7:26

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.