Make WordPress Core

Opened 11 years ago

Closed 11 years ago

#11929 closed enhancement (duplicate)

Query objects by metadata

Reported by: wnorris Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: General Keywords:
Focuses: Cc:


The new metadata API makes it very easy to get metadata for a known object. For example, operations like "Give me the last name for the user #12", or "show me all email addresses that are subscribed to comments on post #4".

However, it does not provide an easy way to look up objects based on metadata keys or values. For example, things like "show me all posts the email address 'foo@…' is subscribed to receive comments for". Or "show me all posts that have a 'mood' custom value set, regardless of value". Or (my personal itch), "what user is associated with the OpenID 'http://foo.com'?"

This kind of querying ability would be immensely useful to plugin authors.

Attachments (1)

query-metadata.php (1.1 KB) - added by wnorris 11 years ago.

Download all attachments as: .zip

Change History (7)

#1 @wnorris
11 years ago

  • Cc wnorris added

Here is my first stab at this functionality. It returns an array of objects of the specified meta_type that have the specified meta_key/meta_value pair. If the value is not specified, it returns objects that have any value for the specified key.

Retrieving the object IDs is simple. But is that enough? It still leaves additional work to the plugin author to actually retrieve the object with the specified ID. I went ahead and added one mechanism for populating the objects themselves using a new filter. It would work fine in the case where only a few objects are returned from the query, or where they are already in the WP cache. However, if a large number of objects are returned that are not in the cache, the performance is a bit less than optimal, since each object is populated individually. I'm not sure how much of a concern that should be.

#2 @wnorris
11 years ago

(and I haven't bothered formatting this as an actual patch for now, but I will later. I'd like to hear other people's thoughts on it first.)

#3 @scribu
11 years ago

It would be a lot easier to follow your proposal if you did submit it as a patch. See how it's done here.

#4 @scribu
11 years ago

Sorry, I thought you had modified wp-includes/meta.php.

#5 @scribu
11 years ago

I think getting the ids of the objects is enough.

It's pretty trivial to call get_post() or whatever afterwards.

#6 @Denis-de-Bernardy
11 years ago

  • Milestone 3.0 deleted
  • Resolution set to duplicate
  • Status changed from new to closed

there are a few tickets that request being able to use LIKE statements on meta fields using WP_Query.

marking as dup of #11482, which is the better approach.

Note: See TracTickets for help on using tickets.