0

I am iterating through an array of post IDs $ids. All is displayed correctly except for the authors. Every post shows the same author, although the posts are by different authors. I can't seem to get this worked out...

<?php 
$ids = array();
$bookid = get_post_meta(get_the_ID(), 'prefered_book', true ); 
$args = array(
    'post_type' => 'post',
    'post__not_in' => array($post->ID),
    'tax_query' => array(
        array(
            'taxonomy' => 'books',
            'field' => 'id',
            'terms' => "$bookid",
        )
    )
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
     while ($my_query->have_posts()) : $my_query->the_post(); 
         $ids[]= $post->ID; 
     endwhile;
    }
?>

<?php
$bookdesc = get_term( $bookid , 'books');

if(count($ids)) : ?>
     <h5>Post about <?php echo $bookdesc->description; ?></h5>
     <ul>
     <?php foreach($ids as $id) : ?>
         <li>
             <a href="<?php echo get_permalink( $id ); ?>">
                 <div class="Wrap">
                 <?php $img = wp_get_attachment_url( get_post_thumbnail_id($id,'size-200') ); ?>
                 <?php if ($img) : ?>
                     <img class="smallImage" src="<?php echo $img ?>" />
                 <?php endif; ?>
                     <h3><?php echo get_the_title($id); ?></h3>
                     <h6>By <?php the_author($id); 
                             // Here it fails, incorrectly showing same author for every post
                     ?></h6> 

                 </div>
             </a>
         </li>
     <?php endforeach; ?>
     </ul>
<?php endif; ?>
  • 1
    Have you tried echoing the ID inside the foreach loop to make sure it's getting the correct ID? The code looks fine, but I don't get the point of running a foreach loop if you already have a while loop with all the data you need. – gdaniel Feb 3 '14 at 23:23
  • You are right about the foreach, that was some old code I hadn't cleaned up. I removed the foreach and put everything inside the while loop, but the result is exactly the same. I am not sure I understand what ID should be echoed? The author ID? print_r($ids); gets all the correct post IDs. – psot Feb 3 '14 at 23:45
2

You need to move the bulk of your code into the loop proper. This part:

$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
  while ($my_query->have_posts()) : $my_query->the_post(); 
     $ids[]= $post->ID; 
  endwhile;
} 

When the_post() executes, it populates the $post global upon which a number of template functions depend. The problem is that you are running your foreach Loop but not resetting that global.

You could use setup_postdata() inside that foreach but I don't see the point. It looks like you can do the whole thing in the first Loop.

  • I followed your instructions and got rid of the foreach and put all the code in the loop but still get only the one author for all posts. I am calling the code in a template after the posts loop in a single.php template. Could that cause the problem? – psot Feb 4 '14 at 1:15
  • Disregard my previous comment. Now it works! Thanks! – psot Feb 4 '14 at 1:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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