Make WordPress Core

Opened 9 years ago

Closed 9 years ago

#14389 closed enhancement (fixed)

Refactor wp_get_recent_posts to use get_posts function

Reported by: blepoxp Owned by: blepoxp
Milestone: 3.1 Priority: normal
Severity: normal Version:
Component: Performance Keywords: has-patch needs testing
Focuses: Cc:


The wp_get_recent_posts function currently only allows 1 argument for number of posts, queries the database directly with $wpdb->get_results( $sql ) and returns the result set as an assc array.

This refactor would retain the above functionality by default as well as make the result set much more flexible, allowing access to all the args available within the get_posts() function.

Attachments (2)

14389.diff (2.0 KB) - added by blepoxp 9 years ago.
wp_get_recent_posts refactored
14389-2.diff (1.9 KB) - added by blepoxp 9 years ago.
Adjusts arguments and replaces foreach cast with get_object_vars

Download all attachments as: .zip

Change History (8)

9 years ago

wp_get_recent_posts refactored

#1 @nacin
9 years ago

  • Component changed from General to Performance
  • Milestone changed from Awaiting Review to 3.1

Instead of deprecating the argument, I think we can just do an is_numeric check on $args, and then turn it into $args = array( 'numberposts' => absint( $args ) ).

Also, I think there's probably a cleaner way to handle the array conversion?

#2 @blepoxp
9 years ago

Good idea. Yeah, I wasn't happy with the foreach loop. I'll look into other options.

#3 @blepoxp
9 years ago

  • Keywords dev-feedback added; needs-testing removed

This is the best solution I found for converting an Object to an array outside of the foreach loop I used but it would include introducing another function. Thoughts? (I couldn't find a wp function that already did this).

function wp_object_to_array( $object ) {
        if ( !is_object( $object ) && !is_array( $object ) )
            return $object;
        if ( is_object( $object ) )
            $object = get_object_vars( $object );

        return array_map( 'wp_object_to_array', $object );

#4 @nacin
9 years ago

We don't currently have a helper for that; we leverage get_object_vars() in a few places... I would do the same.

9 years ago

Adjusts arguments and replaces foreach cast with get_object_vars

#5 @blepoxp
9 years ago

  • Keywords needs testing added; dev-feedback removed

Added updated patch.

#6 @ryan
9 years ago

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

(In [15973]) Refactor wp_get_recent_posts to use get_posts(). Props blepoxp. fixes #14389

Note: See TracTickets for help on using tickets.