Here's a function that does what I need it to do. Someone else wrote it. I want to understand how it is doing it. (More will be added to it FYI.)

The goal here was to get the id of the most recent post. I understand what the WordPress function part is doing. I don't understand what is happening to set the value for $thePostID after the WordPress function has found the first post. In particular, the [0] and [ID].

Can someone please explain?

function prepare_payment() {
  $recent_posts = wp_get_recent_posts( array( 'numberposts' => '1' ) );
  $thePostID = $recent_posts[0]['ID'];
  echo $thePostID;

prepare_payment() is calling wp_get_recent_posts() which returns an array of post by default. Only one post is being returned in this case, due to the $numberposts parameter being set to 1.

$recent_posts holds the array of posts returned by wp_get_recent_posts(). The following line is setting $thePostID to the ID of the first post in the $recent_posts array (arrays are zero indexed):

$thePostID = $recent_posts[0]['ID'];

Finally, the ID is echo'd using the line echo $thePostID;

Edit: Here is an example of the array returned by wp_get_recent_posts(), which itself is a wrapper for get_posts().`

    [0] => Array
            [ID] => 418
            [post_author] => 2
            [post_date] => 2025-01-01 00:00:00
            [post_date_gmt] => 2025-01-01 00:00:00
            [post_content] => This post is scheduled to be published in the future.

It should not be displayed by the theme.
            [post_title] => Scheduled
            [post_excerpt] => 
            [post_status] => future
            [comment_status] => open
            [ping_status] => closed
            [post_password] => 
            [post_name] => scheduled
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2016-04-11 04:28:22
            [post_modified_gmt] => 2016-04-11 04:28:22
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => http://wpthemetestdata.wordpress.com/?p=418
            [menu_order] => 0
            [post_type] => post
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw


You can replicate this by adding a simple debugging statement to prepare_payment():

function prepare_payment() {
  $recent_posts = wp_get_recent_posts( array( 'numberposts' => '1' ) );

    // Temporary debugging statement
    print_r( $recent_posts );

  $thePostID = $recent_posts[0]['ID'];
  echo $thePostID;
  • Thank you, James. I added an example array to the answer. Jan 6 '17 at 2:59
  • THAT is exactly what I needed to know! Tips like that are the true education in learning to code. Reading the Codex only goes so far in giving one an understanding. Seeing the values returned makes it all very clear. Many, many thanks Dave!
    – James
    Jan 6 '17 at 3:05
  • Very happy to help, James! Happy coding! Jan 6 '17 at 3:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.