WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 20 months ago

#16910 new feature request

Lazy evaluation for WP_Query

Reported by: scribu Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Query Keywords: needs-patch
Focuses: Cc:

Description (last modified by scribu)

Suppose we have a 'city' post type, which is associated with a 'country' post type, such that each 'city' post has a 'country' parent post.

Then the 'country' CPT has a 'language' taxonomy associated to it.

Now, let's say we want to find all the cities which speak a certain language.

Let's assume we already have the 'post_parent__in' query var: #13927

We could construct a two-step query, like this:

1) Get all the countries with that language:

$country_ids = get_posts( array(
  'fields' => 'ids',
  'post_type' => 'country',
  'language' => 'french',
  'nopaging' => true
) );

2) Get all the cities belonging to that country:

$cities = get_posts( array(
  'post_type' => 'city',
  'post_parent__in' => $country_ids
) );

No custom SQL queries; fantastic!

But, if you have many many countries (not a good example, I know), you waste a lot of time passing the IDs back and forth from PHP to SQL.

Final query:

SELECT *
FROM wp_posts
WHERE post_type = 'city'
AND post_parent IN (1, 2, 3, ...)

It would be a lot more scalable to put the first query into the second, directly, as a subquery.

So, it would now look like this:

1) Get all the countries with that language:

$country_ids = get_posts( array(
  'fields' => 'ids',
  'post_type' => 'country',
  'language' => 'french',
  'nopaging' => true,

  'lazy' => true'
) );

2) Get all the cities belonging to that country:

$cities = get_posts( array(
  'post_type' => 'city',
  'post_parent__in' => $country_ids
) );

$country_ids would now be a WP_Lazy_Query object, which would just contain the subquery SQL. It would be appended to the second query, when needed:

Final query:

SELECT *
FROM wp_posts
WHERE post_type = 'city'
AND post_parent IN (
  SELECT ID
  FROM wp_posts
  WHERE post_type = 'country'
  INNER JOIN wp_terms ...
)

Change History (6)

comment:1 @scribu4 years ago

  • Description modified (diff)
  • Keywords 2nd-opinion removed

comment:2 @scribu4 years ago

  • Description modified (diff)

comment:3 @scribu4 years ago

  • Description modified (diff)

comment:4 @scribu3 years ago

  • Description modified (diff)

comment:5 @scribu3 years ago

  • Component changed from General to Query
  • Summary changed from Direct subqueries to Lazy evaluation for WP_Query

comment:6 @wonderboymusic20 months ago

  • Keywords needs-patch added

I yearn for things like this

Note: See TracTickets for help on using tickets.