WordPress.org

Make WordPress Core

Opened 10 months ago

Last modified 10 months ago

#51796 new enhancement

Raw post data instead of WP_Post instances

Reported by: screamingdev Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Query Keywords: reporter-feedback
Focuses: performance Cc:

Description

Within WP_Query::get_posts you find:

// Convert to WP_Post objects.
                if ( $this->posts ) {
                        $this->posts = array_map( 'get_post', $this->posts );
                }

This costs a lot of time when fetching a lot of posts (e.g. products, orders, etc).

It could be fasten up by fetching only the field "ids" but this is also bad because when we need the post_content then a "get_post( 123 )" would do another query.

To bypass all of this a new "fields operator" could be added:

                if ( $this->posts && $q['fields'] !== 'raw' ) {
                        $this->posts = array_map( 'get_post', $this->posts );
                }

The SQL-Query would still fetch all data but we would prevent the data from going through the "get_post" stuff.

In times of Generator it is ridiculous to iterate over the same set of data multiple times. By receiving the raw data the developer can decide when to generate a WP_Post object.

Note:
filter = "raw" could imply "suppress_filters" = true because we no longer carry WP_Post objects. But it should be allowed by the developer to switch "suppress_filters = false" again because the stdObjects have almost the same structure as WP_Post objects. This means (in short):

if ( ! isset( $q['suppress_filters'] ) ) {
                        $q['suppress_filters'] = ($q['filter'] === 'raw' ? true: false);
                }

Change History (4)

#1 @SergeyBiryukov
10 months ago

  • Component changed from General to Query

#2 @johnbillion
10 months ago

  • Keywords reporter-feedback added

What's the performance impact of this change?

#3 @screamingdev
10 months ago

What? It creates objects and runs through some filters.
Getting the raw data from the database can be enough in some scenarios (esp. for fetching a lot of posts, products, orders, etc).

#4 @johnbillion
10 months ago

I mean what is the performance benefit of this change when you tested its performance? How much time and/or memory is saved?

Last edited 10 months ago by johnbillion (previous) (diff)
Note: See TracTickets for help on using tickets.