How to display recent posts added in custom post types

admin2025-01-07  9

I have tried a code posted here>>Custom Post Type: Get most recent permalink

But the thing is-it is showing only one post from the recent and without get_the_title,,, can anyone help me to achieve, upto 10 posts' Title and permalink of custom post type.

I am googling from about 1 hour.. Please anyone help me

I have tried a code posted here>>Custom Post Type: Get most recent permalink

But the thing is-it is showing only one post from the recent and without get_the_title,,, can anyone help me to achieve, upto 10 posts' Title and permalink of custom post type.

I am googling from about 1 hour.. Please anyone help me

Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Dec 17, 2013 at 15:22 Praveen KashyapPraveen Kashyap 331 gold badge2 silver badges5 bronze badges 1
  • 2 Please .... show you full code here instead of linking to a tutorial. This is lazy and just adds to the number of questions that suffer from link rot – kaiser Commented Dec 17, 2013 at 15:37
Add a comment  | 

4 Answers 4

Reset to default 13

The answer is essentially in Codex!

<h2>Recent Posts</h2>
<ul>
<?php
    $recent_posts = wp_get_recent_posts(array('post_type'=>'book'));
    foreach( $recent_posts as $recent ){
        echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> </li> ';
    }
?>
</ul>

The only thing I did was add an argument to search for the book post type instead of the default post type.

And this is probably a duplicate of this question anyway, but the system won't let me mark it as such.

Assuming you know how to use WP_Query, you can use following code to get recent 10 posts for any custom post type .

$args = array(
    'post_type' => 'your-custom-post-type',
    'orderby' => 'date',
    'order' => 'DESC',
    'posts_per_page' => 10
     );
$query = new WP_Query( $args ); 

You can use this code to get recent posts

$recent_posts = get_posts(  array( 'posts_per_page' => 1, 'orderby'  => 'post_date', 'order'=> 'DESC', 'post_type' =>  array('camp','cruise','competition','combine')));

Can also add this code. Must add the 'post_status' => 'publish' in the array code.

<?php
$recent_posts = wp_get_recent_posts(array('numberposts' => 100, 'post_status' => 'publish', 'post_type'=>'book'));
foreach( $recent_posts as $recent ){
    echo '<a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> ';
}?>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736253264a111.html

最新回复(0)