6

I'm trying to get posts which have applied the meta value trainees.

$args = array(
    'posts_per_page'   => -1,
    'orderby'          => 'post_date',
    'order'            => 'DESC',
    'post_status'      => 'publish',
    'meta_query' => array(
        array(
            'key'     => 'enterprise_on_page',
            'value'   => array( 'trainees' ),
            'compare' => 'IN',
        ),
    ),
);

$enterprise_posts = get_posts( $args );


In my meta-field with the key enterprise_on_page I have stored an array() previously like update_post_meta( $post_id, 'enterprise_on_page', array('trainees', 'staff') );.

If I call var_dump(get_post_meta($post->ID)) I get the following output:

'enterprise_on_page' => 
    array (size=1)
      0 => string 'a:2:{i:0;s:8:"trainees";i:1;s:7:"staff";}' 


How can i modify my query so that only posts with meta-value trainees are shown. By now I don't get any posts.

0

3 Answers 3

9

Thanks to @Howdy_McGee. He gave me the hint to look after serialized meta queries. With this code I get the desired result.

$args = array(
    'posts_per_page'   => -1,
    'orderby'          => 'post_date',
    'order'            => 'DESC',
    'post_status'      => 'publish',
    'meta_query' => array(
        array(
            'key'     => 'enterprise_on_page',
            'value'   => serialize(strval('trainees')),
            'compare' => 'LIKE',
        ),
    ),
);

$enterprise_posts = get_posts( $args );
3

I don't believe you can accurately compare values for a Serialized Array, which happens whenever you pass an array to update_post_meta, you need to get the entire value during your loop using

get_post_meta( $post->ID, 'enterprise_on_page', 1 )

If you need to specify queries based on these values it's best to save them out separately.

View this question for more information:

meta_query with meta values as serialize arrays

2

As your data is currently structured, you can't use SQL to do that (well, not in a performant way, at least). You have two options:

  1. Fetch ALL posts and loop through them in memory looking for the correct post meta, or
  2. Store the meta values separately.

Between the two, I advise the latter. WordPress permits you to store multiple rows of the same post meta for a single post, so you can get an array of data without needing to serialize it and store it in one key. To do this, you need to slightly change how you're interacting with the post meta api here. Something like this is what you'd want:

When getting the post meta, you'd do

get_post_meta($ID, 'enterprise_on_page', false);

Updating would be the most different. If you want to merely append a value, do

add_post_meta($ID, 'enterprise_on_page', $value);

To update a value,

update_post_meta($ID, 'enterprise_on_page', $new_value, $old_value);

And to remove a value:

delete_post_meta($ID, 'enterprise_on_page', $value_to_delete);

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.