wrong with set compare in meta_query – wordpress

The checkboxs saved in db !

The db structure (wp_postmeta table) :

Read More
________________________________________________________________
|               |                                               |
|   meta_key    |                   meta_value                  |
|_______________|_______________________________________________|
|               |                                               | 
| artist_nameaa |   a:2:{i:51;s:2:"51";i:60;s:2:"60";}          |
|_______________|_______________________________________________|

two checkboxs has been selected !

i want to find posts where artist_nameaa have 51 key in his array

51 is an example

Here is my try :

    wp_reset_postdata();
    global $post;
    $args = array(
        'post_type' => 'songs',
        'meta_query' => array(
            array(
                'key' => 'artist_nameaa',
                'value' => serialize(array($post->ID=>"$post->ID")),// a:1:{i:51;s:2:"51";}

                'compare' => 'IN'
            )
        ),
        'no_found_rows'         => true,
        'update_post_meta_cache'    => false,
        'update_post_term_cache'    => false,
        'ignore_sticky_posts'       => 1,
        'post__not_in'          => array($post->ID),
        'posts_per_page'        => -1
    );

    $query = new WP_Query($args);
    return $query;

Related posts

1 comment

  1. The IN() comparison operator is used to replace several where clauses (docs). It will ultimately be looking for results that are an exact match.

    What you are doing is more of a search. You have no idea where in the serialized string your data set will fall. For this type of search, you should use the LIKE operator (docs). You will need to utilize the wildcard flags for this type of search.

    From what I can gather about the type of data you are storing, you may want to rethink your storage mechanism. Custom Fields do support multiple values for the same meta key, which would be a quick approach. You could also take advantage of Custom Post Types or a custom table (wpdb).

Comments are closed.