简体   繁体   中英

Get the latest posts of the author in the loop

I need to display the last 5 posts for current author in single.php . These posts are registered in the table wp_usermeta as a meta_key ( user_posts ) and meta_value ( a:2:{i:0;s:4:"1336";i:1;s:4:"1334";} ) where 1336 and 1334 are post ids.

I've tried many methods to get more posts of current author, and found no solution.

<?php
 $post_ids = get_user_meta($user_id,'user_posts',true); 
 $posts = get_posts(array(
 'author' => get_the_author_id(), 
 'numberposts' => 5,  
 'orderby' => 'post_date', 
 'post__in' => explode(',', $post_ids)));
  if($posts) { echo '<ul>';
    foreach($posts as $post) { ?>
                <li><a href="<?php echo get_permalink($p->ID) ?>" rel="bookmark"  title="Permanent Link to <?php echo $post->post_title; ?>"><?php echo $post->post_title; ?> </a></li>
    <?php }
    echo '</ul>';} ?>

You can use the following code to fetch the latest 5 posts by current author in single.php

You dont need to parse that serialized string

$authorID = get_the_author_meta('ID');

$authors_posts = get_posts( array( 
     'author' => $authorID, 
     'numberposts' => 5,
     'orderby' => 'date'      
   ) 
 );

Check the two functions doc in wp codex get_posts and get_the_author_meta

You can then loop over the posts to get individual post details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM