• Resolved astromono

    (@astromono)


    Hello!

    I’m having difficulties to get a custom query work with get_recent_posts and a cusotm post type. I want the function to call the most recent post within the “podcasts” CPT, but for some reason the query isn’t working.

    Any help I canget would be greatly appreciated!

    Here’s my code:

    <?php
    $args = array(
    	'numberposts' => 1,
    	'offset' => 0,
    	'category' => 0,
    	'orderby' => 'post_date',
    	'order' => 'DESC',
    	'include' => '',
    	'exclude' => '',
    	'meta_key' => '',
    	'meta_value' =>'',
    	'post_type' => 'podcasts',
    	'post_status' => 'publish',
    	'suppress_filters' => true
    );
    
    $recent_posts = wp_get_recent_posts( $args );
    ?>
    
    	<ul id="podcastbox-wrap">
    		<?php
    	$recent_posts = wp_get_recent_posts();
    	foreach( $recent_posts as $recent ){
    
    	echo '<li id="podcastlist"><a id="podcast-link" style="background-color:#8617ce;" href="' . get_permalink($recent["ID"]) . '">Latest Podcast Episode</a> <img id="podcastbox-image" src="'.get_the_post_thumbnail($recent["ID"], 'large').'"></li> ';
    	}
    	wp_reset_query();
    ?>

    thanks!!!

    • This topic was modified 7 years, 2 months ago by astromono.

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Why use all the unneeded args? Try this instead:

    $args = array(
    	'numberposts' => 1,
    	'orderby' => 'post_date',
    	'order' => 'DESC',
    	'post_type' => 'podcasts',
    	'post_status' => 'publish',
    	'suppress_filters' => true
    );
    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Assuming the CTP slug really is “podcasts’ and not ‘podcast’. (Been there, done that.)

    Ha! I made that mistake before.

    to begin with, do remove the unneeded args;

    also, you are calling the function twice;
    first:
    $recent_posts = wp_get_recent_posts( $args );

    then again wiythout args, therefore implying the default args:
    $recent_posts = wp_get_recent_posts();

    remove that second one…

    Thread Starter astromono

    (@astromono)

    Oh wow, thank you so much for such a quick reply, you guys! And thank you, Michael, that was exactly right! (hear me go DO’H and wake my roommates at 1am).

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘‘get_recent_posts’ Not Working for Custom Post Type’ is closed to new replies.