Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a wordpress theme which needs to retrieve the latest 3 author posts which is simply straight forward but I have ran into a sticky problem.

Because I am calling this from already within the:

if ( have_posts() ) : while ( have_posts() ) :the_post();
endwhile; endif;

For some reason it's not working.

Here is the code I have used:

if ( have_posts() ) : while ( have_posts() ) :the_post();

//some content goes here regarding the post itself!!!
//some content goes here regarding the post itself!!!
//some content goes here regarding the post itself!!!
//some content goes here regarding the post itself!!!
//some content goes here regarding the post itself!!!
//some content goes here regarding the post itself!!!
//some content goes here regarding the post itself!!!
//some content goes here regarding the post itself!!!
//some content goes here regarding the post itself!!!

$relatedargs = array(

'author_name' => get_the_author(),
'post__not_in' => array( $post->ID),
'posts_per_page' => 3

);

$relatedquery = new WP_Query( $relatedargs );

while($relatedquery->have_posts()){ $relatedquery->the_post(); 

?>

<div class="span3">

<?php
if(has_post_thumbnail()) { 
$relatedthumbnail = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'medium', false);
$relatedthumbnail_large = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full', false);
?>

<div class="hover_colour"><a href="<?php echo $relatedthumbnail_large['0']; ?>" rel="prettyPhoto"><img src="<?php echo $relatedthumbnail['0']; ?>" alt="<?php the_title(); ?>" /></a></div>

<?php } ?>

<h6><a href="<?php the_permalink(); ?>"><span><?php the_title(); ?></span></a><br><i class="icon-time muted"></i> <?php echo get_the_time('j') . '/' . get_the_time('m') . '/' . get_the_time('Y') . ' '; ?> <i class="icon-comments muted"></i> <a href="<?php the_permalink(); ?>"> <?php comments_number(0 . __(' comments','textdomain'), 1 . __(' comment','textdomain'), '% ' . __('comments','textdomain')); ?></a></h6>

</div>

<?php } wp_reset_postdata(); ?>

endwhile; endif;

It works 100% on my local page but when I upload to the server it just does not show anything, I get no errors whatsoever, I'm on debug mode.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

I think you are executing reset_postdata() too early, using the global $post object incorrectly inside the secondary loop and passing wrong argumentes to the secondary loop. Try this.

Put the function to get the latest author posts in the functions.php file:

function the_latest_author_posts($post) {

        //some content goes here regarding the post itself!!!
        $relatedargs = array(

             'author' => $post->post_author,
             'post__not_in' => array( $post->ID),
             'posts_per_page' => 3

        );

        $relatedquery = new WP_Query( $relatedargs );

        while($relatedquery->have_posts()){
             $relatedquery->the_post(); 
             $ID = get_the_ID();
        ?>

             <div class="span3">

             <?php
                  if(has_post_thumbnail()) { 
                       $relatedthumbnail = wp_get_attachment_image_src( get_post_thumbnail_id($ID), 'medium', false);
                       $relatedthumbnail_large = wp_get_attachment_image_src( get_post_thumbnail_id($ID), 'full', false);
?>

                       <div class="hover_colour"><a href="<?php echo $relatedthumbnail_large['0']; ?>" rel="prettyPhoto"><img src="<?php echo $relatedthumbnail['0']; ?>" alt="<?php the_title(); ?>" /></a>
                       </div>

                 <?php } ?>

                       <h6><a href="<?php the_permalink(); ?>"><span><?php the_title(); ?></span></a><br><i class="icon-time muted"></i> <?php echo get_the_time('j') . '/' . get_the_time('m') . '/' . get_the_time('Y') . ' '; ?> <i class="icon-comments muted"></i> <a href="<?php the_permalink(); ?>"> <?php comments_number(0 . __(' comments','textdomain'), 1 . __(' comment','textdomain'), '% ' . __('comments','textdomain')); ?></a></h6>

               </div>

    <?php }
    wp_reset_postdata();
}

Then, in your theme, inside the loop, you can call the function wherever you want to display latest posts from the author of the current post:

<?php
if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();
        //some content goes here regarding the post itself!!!
        //some content goes here regarding the post itself!!!
        the_latest_author_posts($post);
    }
}
?>
share|improve this answer
    
Hi, It looks like it was because of the global post. Thanks :) –  Robert Oct 8 '13 at 12:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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