0

I'm adjusting an archive page to only show products where a certain ACF meta field has a specific value but its not straight forward as the meta value is a nested array so I'm struggling to check if the value is in there.

I've commented what get_post_meta(get_the_id(), 'sub_categories'); pulls through, I've tried just the value as in Oil Lubricated tried %Oil Lubricated% etc but no joy.

See below query code.

E.g

...

$meta_query, array(
    'key'=> 'sub_categories',
    'value'=> '', // Array ( [0] => Array ( [0] => Variable Speed Frequency Controller [1] => Oil Lubricated ) )
    'compare'=>'LIKE' // = / exists / like 
);
$wp_query->set('meta_query',$meta_query);

...

Any help would be greatly appreciated.

Cheers

4
  • 3
    This isn't possible, you can't safely query the insides of serialised data structures stored as strings in post meta. Workarounds are slow and extremely unreliably. Is there a reason you didn't use a custom taxonomy for this? It would be significantly faster easier and more reliable than a serialised array. Even separate values would be easier. You will need to change how you store this data
    – Tom J Nowell
    Apr 27, 2021 at 11:28
  • I would have used a custom taxonomy but its what oim dealing with right now.
    – DonnyDev
    Apr 27, 2021 at 12:03
  • 1
    To get what you want a data migration of some sort is mandatory and unavoidable, this is the best time to transition to a taxonomy, if not multiple post meta with the same name. Taxonomies will give you a massive performance boost over the meta_query approach, they're much friendlier and lighter on the database for searches, have dedicated APIs, REST API endpoints, free admin UI, they have templates and archives, and can have term meta and children if you desire it. I'd strongly recommend creating a custom taxonomy for this, then asking a new Q for how to migrate your existing data
    – Tom J Nowell
    Apr 27, 2021 at 17:21
  • 1
    To be crystal clear, what you want is not possible as long as the data is stored this way. For more information on why see wordpress.stackexchange.com/questions/16709/…, infact this is a duplicate of that question
    – Tom J Nowell
    Apr 27, 2021 at 17:22

0

Browse other questions tagged or ask your own question.