Check if string exists in meta key string using meta_query check in wordpress get_posts function

OK you would think this is a fairly trivial piece of code, but it is not working.

The code below is not broken, it is just not working as it should.

Read More

Ok, in wordpress I have a custom field in my custom post-type: individual, this custom field has a meta_key of: login_email

The contents of this meta_key is simply email addresses.

Here are some examples of the contents..

bob.dougal@gmail.com, bob.dougal@company.com, bob.dougal@hotmail.com

or..

bob.dougal@gmail.com, bob.dougal@hotmail.com

or even just a single email string..

bob.dougal@hotmail.com

OK, so now you seen the string contents of the custom field. I can explain what I am trying to achieve.

I have this variable… $current_user_email = $current_user->user_login;

Which is a single email address string.

I then need to find if this email address exists within the meta_key contents. So this how I’ve done it…

$lastposts = get_posts(array(
    'posts_per_page'    => 1,
    'post_type'         => 'individual',
    'post_status'       => 'private',
    'meta_query'        => array(
        array(
            'key' => 'login_email',
            'value' => $current_user_email,
            'compare' => 'IN'
        )
    )
));

In my example above, if I echo $current_user_email it outputs bob.dougal@company.com

But even though bob.dougal@company.com exists in one of the custom fields like this… bob.dougal@gmail.com, bob.dougal@company.com, bob.dougal@hotmail.com, my get_post returns nothing.

If I then go to the post editor and remove all the other emails from the custom field so bob.dougal@company.com is the only text in the custom field, then the above query works!

My question is, how can I get meta_query to find bob.dougal@company.com within a meta_key which contains this string: bob.dougal@gmail.com, bob.dougal@company.com, bob.dougal@hotmail.com

Because ‘IN‘ is not doing what it is meant to.

Code taken from http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

Thanks

Related posts

Leave a Reply

2 comments

  1. I believe this may be happening because you are storing multiple email addresses in one key, as opposed to having multiple keys with one email address each.

    Because of this, IN will not work as it checks if a given value exists within a set of possibilities (similar to how the PHP in_array() works).

    What you need to do is replace IN with LIKE. This will check the whole string for occurrences of your email address and return matches.

    You should be careful though, as this can lead to inconstant results. For example, if $current_user_email = bob.dougal@company.com; then all of the following will be true –

    bob.dougal@gmail.com, 1bob.dougal@company.com, bob.dougal@hotmail.com
    bob.dougal@gmail.com, 12bob.dougal@company.com, bob.dougal@hotmail.com
    bob.dougal@gmail.com, 123bob.dougal@company.com, bob.dougal@hotmail.com
    

    I’ll grant you that it is unlikely, but that’s why you should not use strings to store multiple values. I’d strongly suggest using multiple keys (you can have many with the same name) and then you can use IN in your query. This would also guarentee only exact matches.

  2. $current_user_email is a string. So transform this string into an array:

    $current_user_email = explode(", ", $current_user_email);
    
    $lastposts = get_posts(array(
        'posts_per_page' => 1,
        'post_type' => 'individual',
        'post_status' => 'private',
        'meta_query' => array(
             array(
                 'key' => 'login_email',
                 'value' => $current_user_email,
                 'compare' => 'IN'
             )
        )
    ));