fidev
fidev

Reputation: 1252

Wordpress - display 3 related posts regardless of the post type, custom or otherwise

I'm trying to display 3 posts below my single post view (I have custom post types set up so want this query to work on all single post pages regardless of the post type).

However with the code below I don't get any related posts displayed. When I removed .'&exclude=' . $current I get 3 related posts display but with the current post being one of them. Hence why I added 'exclude' but I don't see why its not displaying any when I add this in.

Any help would be appreciate. Thanks

<?php

$backup = $post; 
$current = $post->ID; //current page ID

global $post;
$thisPost = get_post_type(); //current custom post
$myposts = get_posts('numberposts=3&order=DESC&orderby=ID&post_type=' . $thisPost .  
'&exclude=' . $current);

$check = count($myposts);

if ($check > 1 ) { ?>
<h1 id="recent">Related</h1>
<div id="related" class="group">
    <ul class="group">
    <?php   
        foreach($myposts as $post) :
            setup_postdata($post);
    ?>
        <li>
            <a href="<?php the_permalink() ?>" title="<?php the_title() ?>" rel="bookmark">
                <article>
                    <h1 class="entry-title"><?php the_title() ?></h1>
                    <div class="name-date"><?php the_time('F j, Y'); ?></div>
                    <div class="theExcerpt"><?php the_excerpt(); ?></div>
                </article>
            </a>
        </li>

    <?php endforeach; ?>
    </ul>
<?php
    $post = $backup; 
    wp_reset_query();
?>

</div><!-- #related -->
<?php } ?>

Upvotes: 2

Views: 6112

Answers (1)

MrZiggyStardust
MrZiggyStardust

Reputation: 733

Instead of using get_posts(), you could use WP_Query

<?php

// You might need to use wp_reset_query(); 
// here if you have another query before this one

global $post;

$current_post_type = get_post_type( $post );

// The query arguments
$args = array(
    'posts_per_page' => 3,
    'order' => 'DESC',
    'orderby' => 'ID',
    'post_type' => $current_post_type,
    'post__not_in' => array( $post->ID )
);

// Create the related query
$rel_query = new WP_Query( $args );

// Check if there is any related posts
if( $rel_query->have_posts() ) : 
?>
<h1 id="recent">Related</h1>
<div id="related" class="group">
    <ul class="group">
<?php
    // The Loop
    while ( $rel_query->have_posts() ) :
        $rel_query->the_post();
?>
        <li>
        <a href="<?php the_permalink() ?>" title="<?php the_title() ?>" rel="bookmark">
            <article>
                <h1 class="entry-title"><?php the_title() ?></h1>
                <div class="name-date"><?php the_time('F j, Y'); ?></div>
                <div class="theExcerpt"><?php the_excerpt(); ?></div>
            </article>
        </a>
        </li>
<?php
    endwhile;
?>
    </ul><!-- .group -->
</div><!-- #related -->
<?php
endif;

// Reset the query
wp_reset_query();

?>

Try out the code above and modify it for your own need. Modified to suit your own markup.

Upvotes: 4

Related Questions