Reputation: 422
I want to show related posts in Wordpress. I need to select these posts manually, not choose from a category or tags automatically. I have previously used this code:
<?php $orig_post = $post;
global $post;
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=>5, // Number of related posts that will be shown.
'caller_get_posts'=>1
);
$my_query = new wp_query( $args );
if( $my_query->have_posts() ) {
echo '<div id="relatedposts"><h3>Related Posts</h3><ul>';
while( $my_query->have_posts() ) {
$my_query->the_post(); ?>
<li><div class="relatedthumb"><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a></div>
<div class="relatedcontent">
<h3><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
<?php the_time('M j, Y') ?>
</div>
</li>
<? }
echo '</ul></div>';
}
}
$post = $orig_post;
wp_reset_query(); ?>
This selects nicely from the related tags but I need to call my related posts manually.
I have put the post ids that I want to call in the custom field 'myrelatedposts' as a comma separated list, eg: 103, 104, 105, 122
Now I need to call them in the above script.
How can I explode this list of posts into the array (still limiting to 5 posts) then call each post thumbnail and title?
Thanks for any suggestions.
Upvotes: 0
Views: 1604
Reputation: 25312
You should try with these arguments :
$args=array(
'post__in' => explode(',', get_post_meta($post->ID, 'myrelatedposts')),
'ignore_sticky_posts'=>1 // caller_get_posts is deprecated
);
To add post thumbnails in your loop, you simply have to use post thumbnails functions, e.g. from codex :
// check if the post has a Post Thumbnail assigned to it.
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
Upvotes: 2
Reputation: 184
There is plugin called Similar Posts.
http://wordpress.org/extend/plugins/similar-posts/
it searches and match post contents, not only tags.
i think one of the disadvantages is that it runs many slow SQL queries.
Upvotes: 0