Make WordPress Core

Opened 21 months ago

Last modified 21 months ago

#55889 new feature request

Allow specifying multiple keys as an array in meta and option retrieval functions.

Reported by: namithjawahar's profile namith.jawahar Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Options, Meta APIs Keywords:
Focuses: performance Cc:

Description

Allow specifying multiple keys as an array in meta and option retrieval functions like get_option and get_post_meta.

Currently when we have to get multiple meta values from multiple keys we have to use get_post_meta without mentioning the key to retrieve everything or make multiple calls to the function for each key. It will be really helpfull and a lot more efficient if one can specify the keys in the function call as follows
get_post_meta(post_id, array('key_1, 'key_2'...))

Same applies to get_option.

Change History (2)

#1 follow-up: @knutsp
21 months ago

Hello and welcome to Trac!

Thank you for the suggestion. Might be a good idea, or a new function (multi_get_*).

Sure, we often need to get values of multiple keys from meta or options.
I use

foreach ( array('key_1, 'key_2'...) as $key ) {
    $meta_values[ $key ] = get_post_meta( $post_id, $key, true );
}

Why do you think would it be more efficient to specify multiple keys in the function call? The number of SQL queries generated when not cached?

#2 in reply to: ↑ 1 @namith.jawahar
21 months ago

Replying to knutsp:

Why do you think would it be more efficient to specify multiple keys in the function call? The number of SQL queries generated when not cached?

We can reduce the number of SQL calls (when not cached), the reduction should be drastic when the number of fetched fields is high. Also we can get rid of the horrible practice of having to save meta values and options in arrays and serialized into the meta value field. Developers do this to save of database calls.

The update will need to go as far as the SQL query in update_meta_cache() and let it support an additional WHERE parameter for the meta keys in addition to the current IDs.

Note: See TracTickets for help on using tickets.