Opened 3 years ago
Closed 3 years ago
#14389 closed enhancement (fixed)
Refactor wp_get_recent_posts to use get_posts function
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| 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)
Change History (8)
- 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.
- Keywords needs testing added; dev-feedback removed
Added updated patch.

wp_get_recent_posts refactored