get_posts query returning the correct number of results, but values are for current page.

[ad_1]

Hello

I am trying to figure out how to create a query which will generate the link for the most recent newsletter posted to post type newsletter. I am working with the following code snippet, which is returning the correct number of results, but not displaying any of the values, and get\_permalink is showing the current page.

add_shortcode(‘youth_newsletter_shortcode’, ‘youth_newsletter’);
function youth_newsletter(){
$youth_newsletter = get_posts(array(
‘post_type’ => ‘newsletters’,
‘fields’ => ‘ids’,
‘orderby’ => ‘post_date’,
‘order’ => ‘DESC’,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘newsletter’,
‘field’ => ‘slug’,
‘terms’ => ‘youth’
)
),
));
if (! empty ($youth_newsletter)){
$output = count($youth_newsletter) . ‘<br />’;
$output .= ‘<ul>’;
foreach ($youth_newsletter as $post){
$output .= ‘<li>post:’ . get_permalink( $post->ID) . ‘</li>’;
}
$output .= ‘</ul>’;
}

return $output;

}

I currently have 2 newsletters with the taxonomy ‘youth’. I am displaying this shortcode on a page called test-output, and this is the permalink it is displaying, but it is showing 2 lines of this permalink. If I try to do `$output .= ‘<li>post:’ . $post->post_title . ‘</li>’;`, I get two blank lines.

Any thoughts on what I’m doing wrong here?

[ad_2]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer