1

I am trying to run custom loop for Custom Post Type with filtering by some meta_key and value

Now simple meta value works fine but here I have a challenge with the below kind of serialized data (nested).

I am using wpalchemy for meta box. meta_key for the post type is _event_meta and value is as below

a:9:{s:19:"ac_event_operations";a:1:{i:0;s:8:"Training";}s:18:"ac_event_positions";a:1:{i:0;s:10:"Supervisor";}s:18:"ac_event_employees";a:1:{i:0;s:2:"15";}s:13:"ac_event_date";s:10:"2017-06-15";s:19:"ac_event_start_time";s:5:"06:30";s:17:"ac_event_end_time";s:5:"07:00";s:14:"ac_event_place";s:6:"Office";s:18:"ac_event_organizer";s:4:"Jack";s:16:"ac_event_contact";s:4:"Rose";}

I am trying to filter All Events based on ac_event_operations, ac_event_positions, ac_event_employees

So for me, the challenge is to get events filtering with the value from above meta_keys. Here is the query I used but of course, it is not giving any result.

global $event_mb;
$meta = get_post_meta( get_the_ID(), $event_mb->get_the_id(), TRUE );

$paged    = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args     = [
    'post_type'      => 'event',
    'posts_per_page' => get_option( 'posts_per_page' ),
    'paged'          => $paged,
    'meta_query'     => [
        'meta_key' => '_event_meta',
        'value'    => '%ac_event_employees%',
        'compare'  => 'LIKE'
    ],

];
$temp     = $wp_query;
$wp_query = NULL;
$wp_query = new WP_Query( $args );

1 Answer 1

1

philosophical opinion, people that store serialized data do not mean it to be used in searches.

If you need to search in that data, you should hook on the save_post hook (or whatever is relevant) and store whatever you need in a different meta in a way that can be used in search.

Side notes:

  1. Searching in meta is not scalable
  2. LIKE have horrible performance, if you get into the point of using it you should ask yourself where did you go wrong ;)
4
  • I understand what you say. I didn't realize before when I started using WPAlchemy library for the meta-box. Now I realized and learned that creating meta-boxes is the best option. That will gives maximum control in every aspect. Thanks for your input. Commented Jun 13, 2017 at 4:54
  • Have you find a soultion for that? I got into same place that some meta keys that is stored as serialized data needed to be searched/filtered. Commented Oct 22, 2018 at 23:19
  • @MaorBarazany I think that the only option is to "unpack" the serialized data with every field gaining its own meta field. In the long run this is the sanest option, but obviously this might be easier said then done if the code creating the meta is not under your control Commented Oct 23, 2018 at 4:02
  • Well, it appears that if you want only to check if a meta key exists inside the serialized array, and you don't matter for it's value, you may easily treat that serialized array of metakey as a string for this matter, and then in your WP meta_query you may use the key name as the key of the serialized array, the value as the key name that inside the serialized array of keys/values, and compare by LIKE. Commented Oct 24, 2018 at 9:36

Your Answer

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

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