Make WordPress Core

Opened 12 years ago

Closed 12 years ago

#21110 closed defect (bug) (invalid)

Post Dates in wp_get_recent_posts list

Reported by: oqm4's profile oqm4 Owned by: oqm4's profile oqm4
Milestone: Priority: normal
Severity: normal Version: 3.4
Component: General Keywords:
Focuses: Cc:

Description (last modified by SergeyBiryukov)

Using WP 3.3.2, the following statement worked perfectly:

<?php
$args = array( 'numberposts' => '3', 'post_status' => 'publish' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li id="date"><a href="' . get_permalink($recent["ID"]) . '" title="'.esc_attr($recent["post_title"]).'" >' .  get_the_date('M d') . '</a></li><li><a href="' . get_permalink($recent["ID"]) . '" title="'.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> - '.esc_attr($recent["post_excerpt"]).'</li><li id="breaker"></li>';
}
?>

However, since upgrading to 3.4 -- and subsequently to 3.4.1 -- the output contains the date on which the list appears vs. the individual post dates. For example,

  • Apr 13 - Post Title - Post Summary
  • Apr 13 - Post Title - Post Summary
  • Apr 13 - Post Title - Post Summary

vs.

  • Jun 13 - Post Title - Post Summary
  • May 10 - Post Title - Post Summary
  • Mar 01 - Post Title - Post Summary

Change History (6)

#1 @ocean90
12 years ago

  • Description modified (diff)

#2 @SergeyBiryukov
12 years ago

  • Description modified (diff)

#3 @SergeyBiryukov
12 years ago

  • Keywords close added; dev-feedback needs-codex removed

This code works the same way for me on both 3.3.2 and 3.4.1.

As stated in Codex, get_the_date() relies on $post global, which this code doesn't set. This results in the date of the first or the last post in the main (or some other) loop being displayed, depending on where the code is called from.

Setting $post inside the foreach() loop makes it work as expected:

$args = array( 'numberposts' => '3', 'post_status' => 'publish' );
$recent_posts = wp_get_recent_posts( $args );
global $post;
foreach( $recent_posts as $recent ){
$post = (object) $recent;
echo '<li id="date"><a href="' . get_permalink($recent["ID"]) . '" title="'.esc_attr($recent["post_title"]).'" >' .  get_the_date('M d') . '</a></li><li><a href="' . get_permalink($recent["ID"]) . '" title="'.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> - '.esc_attr($recent["post_excerpt"]).'</li><li id="breaker"></li>';
}
wp_reset_postdata();

#4 @oqm4
12 years ago

  • Resolution set to fixed
  • Status changed from new to closed

#5 @coffee2code
12 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

#6 @coffee2code
12 years ago

  • Keywords close removed
  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from reopened to closed
Note: See TracTickets for help on using tickets.