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
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.