With raw SQL through $wpdb, I believe that I could do something along the lines of this (untested):

SELECT post_id, meta_value
FROM postmeta
WHERE meta_key = '_wp_attachment_metadata'
AND meta_value LIKE '%:"custom_key";a:%'

That said, if there is built-in functionality for this kind of query then I would rather not reinvent the wheel. All of my codex digging has just returned ways to get the attachment metadata for a given id or set of ids, which will not do what I need. I could also retain a list of IDs with this particular array key, but I'd rather not keep this data since it seems like extra clutter...

  • 1
    You can do this with WP_Query by making use of the meta_query parameter. – Nicolai Mar 2 '14 at 19:58
  • Right you are! Had forgotten meta_query allowed for LIKE comparison. If you post as answer I'll accept it. – Dan Mar 2 '14 at 20:01

While you can do this with WP_Query, I'd do this the way you are trying to avoid-- save a list in a separate key. The "pros" strongly outweigh the "cons" in my mind.

  1. LIKE queries with wildcards, especially with leading wildcards, are not efficient-- aka, "slow"
  2. and a LIKE query on serialized data is going to be prone to error
  3. You will likely be running this query on display, which means that users see a delay


  1. The "space" needed to store another value in, say, the options table is negligible
  2. And the query to retrieve the value is as about as efficient as a query can be
  3. The work is pushed almost entirely to the backend, when the data is saved, so that any delay is seen by the administrators/authors of the site and not by the ordinary users.

You can add and retrieve the data with the core functions update_option and get_option, the latter of which has caching mechanisms built in.

  • You make a lot of good points. The only reason I was steering away from this option is that I would only ever be running this query when admins are configuring my plugin. When being displayed on the frontend, these values would be accessed via wp_get_attachment_metadata, meaning that retaining a list of IDs would only speed up admin functionality. Does this change your opinion at all? – Dan Mar 3 '14 at 1:51
  • 1
    @Dan: It makes one of the points irrelevant, but not the others. I'd still do it this way. – s_ha_dum Mar 3 '14 at 2:56

You can do this with WP_Query by making use of the meta_query parameter. Do it like shown below. If you use attachment as value for the post_type parameter, remember this:

The default WP_Query sets 'post_status'=>'publish', but attachments default to 'post_status'=>'inherit' so you'll need to explicitly set post_status to 'inherit' or 'any' as well. (See post_status, below)

As stated in the Type Parameter Section of the WP_Query codex page.

$args = array(  
    'post_type' => 'attachment',  
    'post_status' => 'inherit',  
    'post_per_page' => -1,
    'meta_query' => array(  
            'key' => '_wp_attachment_metadata',  
            'value' => 'your-key',  
            'compare' => 'LIKE'  
$query = new WP_Query( $args );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.