WP Query Meta Value – How To Identify Specific, Unique Values?

You are Storing multiple values into one single meta entry, instead of storing each value on its own meta entry (which would be the optimal approach).

But still you can make it work that way.

According to the docs, for the WP Query match operator you can use:

Possible values are ‘=’, ‘!=’, ‘>’, ‘>=’, ‘<‘, ‘<=’, ‘LIKE’, ‘NOT LIKE’, ‘IN’, ‘NOT IN’, ‘BETWEEN’, ‘NOT BETWEEN’, ‘EXISTS’

  • If you go with the = operator, you will not get your expected results since you are storing the club name as a substring.

  • If you go with the LIKE operator, you will get unwanted results if your Club Name is a part of another Club Name. As Cove and Coventry.

So a possible solution if you don’t want to store the names as individual entries would be to force your checks by adding a separator to your $clubname variable.

For example:

'meta_query' => array('relation' => 'OR',
   array('key'=> 'previous',
         'value' => $clubname.',',
         'compare' => 'LIKE',
        ),
   array('key'=> 'later',
         'value' => $clubname.',',
         'compare' => 'LIKE',
        ),
   )

But this will only work if all of your club names do have a comma after it. Including the last name in the list!

It won’t work with spaces because that would not detect the difference between Vauxhall Motors and Vauxhall Motors (London)

So, in this case, your meta value content should be:

“Whatever, something else, blablabla, Vauxhall Motors, other,”

But the real nice solution would be to have a meta field that contains the exact name, and then you can use the = with no issues.