WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 3 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:

Description

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 4 years ago.
wp_get_recent_posts refactored
14389-2.diff (1.9 KB) - added by blepoxp 4 years ago.
Adjusts arguments and replaces foreach cast with get_object_vars

Download all attachments as: .zip

Change History (8)

blepoxp4 years ago

wp_get_recent_posts refactored

comment:1 nacin4 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?

comment:2 blepoxp4 years ago

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

comment:3 blepoxp4 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 );
}

comment:4 nacin4 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.

blepoxp4 years ago

Adjusts arguments and replaces foreach cast with get_object_vars

comment:5 blepoxp4 years ago

  • Keywords needs testing added; dev-feedback removed

Added updated patch.

comment:6 ryan3 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.