0
[province] => Array ( [0] => a:1:{i:0;s:2:"FL";} )

We're using magic fields and I want to do a meta_query on the above, but it doesn't work.

  $query = new WP_Query(array(
    'post_type' => 'location',
    'posts_per_page' => -1,
    'orderby' => 'title',
    'order' => 'ASC',
    'meta_query' => array(
      array('key'   => 'province', 'value' => 'FL', 'compare' => 'LIKE')
    )
  ));

How do I make it work? It returns nothing when I do 'CA' for instance when it should return two. It works with fields that are not encrypted. Is there a trick to this?

2
  • 1
    That field isn't encrypted, it's serialised PHP. you shouldn't store structured data such as objects or arrays in post meta as you can't query the values reliably if at all. E.g. if your query worked it would also match FLA OR LFL. You will need to modify your save code to prevent serialisation so that only CA gets stored, not array( "CA" )
    – Tom J Nowell
    May 19, 2021 at 23:09
  • 1
    Also are you sure you want to store it as a post meta value and not a term named CA in a taxonomy called province? meta_query is particularly slow as the post meta table wasn't designed for searches, so this query will get more and more expensive as you add more posts and meta values. A custom province taxonomy fixes that, and even gives you archives and templates for free as well as other things that can be turned off if you don't want them
    – Tom J Nowell
    May 19, 2021 at 23:11

0

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.

Browse other questions tagged or ask your own question.