Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Check if string exists in metakey string using meta_query in... WordPress

  • SOLVED

<strong>Check if string exists in metakey string using meta_query in get_posts</strong>



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 multiple email addresses or just a single email.

Here are some examples of the contents..

<strong>[email protected], [email protected], [email protected]</strong>

or..

<strong>[email protected], [email protected]</strong>

or even just a single email string..

<strong>[email protected]</strong>




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


I have this variable... <strong>$current_user_email = $current_user->user_login;</strong> ...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 <strong>$current_user_email</strong> it outputs <strong>[email protected]</strong>

But even though <strong>[email protected]</strong> exists in one of the custom fields as this... <strong>[email protected], [email protected], [email protected]</strong>, my get_post returns nothing!!!

If I then go to the post editor and remove all the other emails from the custom field so <strong>[email protected]</strong> is the only text in the custom field, then the above query works!

My question is, how can I get meta_query to find <strong>[email protected]</strong> within a meta_key which contains this string: <strong>[email protected], [email protected], [email protected]</strong>

Because <strong>'IN'</strong> is not doing what it is supposed to do.


Can anyone help?

Answers (2)

2013-12-10

Dbranes answers:

If you have a custom post field <em>login_email</em>, with for example this value:

[email protected], [email protected], [email protected]


then you can try the <strong>LIKE </strong>comparison:


$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' => 'LIKE'
)
)
));


The query will then contain this part:


...cut...
wp_postmeta.meta_key = 'login_email'
AND CAST( wp_postmeta.meta_value AS CHAR ) LIKE '%[email protected]%'
...cut...


if the current email is for example:

$current_user_email = [email protected]

<strong>ps: </strong>

If you have a current email that is for example:

[email protected]


then you would get a match for

[email protected]


You should therefore considering adding a separator into the meta value, like this:

,[email protected], [email protected], [email protected],


and use for example this kind of query:



$seperator = ',';

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


Another option would be to add the meta values multiple times for each post ( with only a single email into each meta value) and then use this query:


$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' => '='
)
)
));


Josh Cranwell comments:

I can't believe like actually works.

It seems like is checking wether the email exists in the string.

This is what I am after - thank you very much

2013-12-10

Sébastien | French WordpressDesigner answers:

$current_user_email is a string
transform this string into array above your code and it's ok


$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'
)
)
));


try it and tell me if it's ok


Josh Cranwell comments:

Hi Thanks for your answer, but $current_user_email is always just a single email.

Dbraines answer below worked for me.


Thanks for your help anyway.