WordPress.org

Make WordPress Core

#43636 closed enhancement (duplicate)

Additional wp_query 'fields' choices

Reported by: mattkeys Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Query Keywords: has-patch has-unit-tests
Focuses: performance Cc:

Description

Currently wp_query accepts an option called 'fields' with possible values of: ids or id=>parent.

I propose that two new options be added: author_ids, titles.

My current use case is that I am needing to populate a list of all author ids that meet certain post type and taxonomy parameters. I only need the author ids and would like to exclude querying all of the fields for performance reasons.

Looking through the wp_posts table the only other column that I think there is a good use case for is the post_title column.

Attachments (4)

wp_query_fields.diff (1.8 KB) - added by mattkeys 18 months ago.
Patch for the proposed enhancement
wp_query_fields_with_unit_tests.diff (3.0 KB) - added by mattkeys 18 months ago.
wp_query_fields + unit tests
wp_query_fields_with_unit_tests_new_field_names.diff (3.0 KB) - added by mattkeys 18 months ago.
Updating the field names to match existing conventions
clean_up_unit_test.diff (3.0 KB) - added by mattkeys 18 months ago.
Same as the last patch but with cleaner unit test code

Download all attachments as: .zip

Change History (11)

@mattkeys
18 months ago

Patch for the proposed enhancement

#1 @mattkeys
18 months ago

I've submitted a patch that I've developed/tested locally. One thing I was not 100% positive about was the use of sanitize_text_field() on the post_title value from the database. Both if this is necessary here, and if this is the correct function to use on this column. Feedback would be great.

This ticket was mentioned in Slack in #core by mattkeys. View the logs.


18 months ago

#3 @mattkeys
18 months ago

  • Keywords has-patch needs-unit-tests dev-feedback needs-testing added

@mattkeys
18 months ago

wp_query_fields + unit tests

#4 @mattkeys
18 months ago

  • Keywords has-unit-tests added; needs-unit-tests removed

@mattkeys
18 months ago

Updating the field names to match existing conventions

#5 @mattkeys
18 months ago

New patch today updating the names of the new field options to match existing convention. Now all of the options for the fields argument are:

ids (existing option)
id=>parent (existing option)
id=>author_id (new)
id=>title (new)

@mattkeys
18 months ago

Same as the last patch but with cleaner unit test code

#6 @boonebgorges
17 months ago

Hi @mattkeys - Thanks for the ticket and for the patches.

The idea to introduce additional values for fields has come up numerous times in the past. See #19866, #33519, #14777. The structural problem is that fine-grained SELECT fields pose problems for the post cache. See https://core.trac.wordpress.org/ticket/19866#comment:6 and follow-up discussion.

Post queries are a somewhat special case, because the posts table has a couple of longtext fields that can get very large and lead to performance problems that are quite real (as opposed to some microoptimizations that you might achieve by not querying post_author, post_parent, etc). A more promising strategy might therefore be to have a way of *excluding* certain fields. See https://core.trac.wordpress.org/ticket/19866#comment:21. So, you'd have a flag (or multiple flags?) for excluding post_content, perhaps post_excerpt. And this flag would have to play reasonably nicely with post caches: use the values from the cache where available, but never populate the individual post cache with incomplete entries.

#7 @ocean90
16 months ago

  • Keywords dev-feedback needs-testing removed
  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed
  • Version 4.9.5 deleted

Closing as a duplicate of #19866 to keep the discussions at one place.

Note: See TracTickets for help on using tickets.