I have array in post_meta like this:

    'option1' => 'true',
    'option2' => 'true',
    'option3' => 'false',
    option4' => 'true',
    'option5' => 'false'

and I would like to filter option1 with value true. Can someone help me?


No, you can't do that via a query. You can request everything then manually filter out items via PHP, but that would be slow.

Instead, you need to address the root problem, that you're storing serialised PHP data in a single field, rather than multiple fields, e.g. you should have an option1 meta key/value and an option2 meta key value pair etc

As a sidenote, if you're wanting to use these to filter, perhaps you should be using a taxonomy instead of post meta

| improve this answer | |

I think you can do it with the meta_compare option:

$args = array(
 'meta_key'     => 'option1',
 'meta_value'   => true,
 'meta_compare' => 'LIKE'

See: https://codex.wordpress.org/Class_Reference/WP_Meta_Query

| improve this answer | |
  • This would only be if the data is stored as a different meta_keys for each of "option1", "option2" etc. If the values are stored under one key wordpress will serialize them as it's way of storing an array as a string. – Bysander May 16 '18 at 18:45

Your Answer

