Opened 3 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
Priority: normal Milestone: 3.1
Component: Performance Version:
Severity: normal Keywords: has-patch needs testing
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 3 years ago.
wp_get_recent_posts refactored
14389-2.diff (1.9 KB) - added by blepoxp 3 years ago.
Adjusts arguments and replaces foreach cast with get_object_vars

Download all attachments as: .zip

Change History (8)

blepoxp3 years ago

wp_get_recent_posts refactored

  • 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?

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

  • 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 );
}

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

blepoxp3 years ago

Adjusts arguments and replaces foreach cast with get_object_vars

  • 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.