0

I find myself facing a situation where I will have a bunch of custom post types and I want to select those with specific meta values. There is a slight wrinkle in that the value I want to select against is within a larger array stored to a single meta key.

I'm pretty sure I could write an SQL select statement using LIKE "%$myterm:$myvalue%" but I wondered if there was an easier way?

2
  • 1
    Not an answer to the question, but if not storing the data in an array is possible, then do that. When storing an array it's saved in a "serialised" form, which is not designed for querying. You've have a much much easier time (and much better performance to boot) if you save the values separately. Jun 12, 2019 at 15:15
  • That's what I suspected. The data is not just foo=>bar but foo=>(bar=>baz,lar=>nar) where foo, baz, and nar could literally be anything. I'm halfway tempted to rejig it with a custom table - at least as an index of the foo vs baz. Jun 12, 2019 at 15:26

1 Answer 1

1

I think I am understanding your correctly...If I'm way off, let me know!

// Assuming ideas is an array and you are looking for ideas=>two
$query = new WP_Query(
    array(
        // You can pass 'any' to post_type, but you get a hold of revisions, attachments, and menu_items. But I am not sure how many Custom Post Types you have ¯\_(ツ)_/¯ 
        'post_type' => array('cpt_one', 'cpt_two', 'cpt_three', 'etc'),
        'meta_query' => array(
            array(
                'key' => 'ideas',
                'value' => array('one', 'two', 'three'),
                'compare' => 'IN'
            )
        )
    )
);

// ideas is still an array here, and the value of 'two' lives inside of that ideas array() 
$query = new WP_Query(
    array(
        'post_type' => array('cpt_one', 'cpt_two', 'cpt_three', 'etc'),
        'meta_query' => array(
            array(
                'key' => 'ideas',
                'value' => 'two',
                'compare' => 'LIKE' // Less-known and not explained well in the docs.
            )
        )
    )
);

Going back to Jacob Peattie's point, querying can be heavy at times depending on the complexity of your arrays. Again, if I am off here....let me know!

3
  • From what I can tell, you are spot on. I am starting to see what you have all been saying about heavy queries. I think the best bet for me is to take this, play with it, and then profile how long it takes on my test server (aka my desktop). At least I know it can be done this way (even if it might not be wise). Jun 12, 2019 at 17:19
  • 1
    codex.wordpress.org/Class_Reference/WP_Meta_Query allows you to get pretty crazy with filters and can give you back some raw SQL to use instead. It will cut out some of the fluff that WP_Query comes shipped with. Jun 12, 2019 at 17:28
  • Wowsers. That page is getting added to my bookmarks. Jun 12, 2019 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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