Opened 7 years ago
Closed 7 years ago
#43636 closed enhancement (duplicate)
Additional wp_query 'fields' choices
Reported by: |
|
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)
Change History (11)
#1
@
7 years 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.
7 years ago
#5
@
7 years 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)
#6
@
7 years 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.
Patch for the proposed enhancement