Question

On my Club Profile pages, I'm displaying a list of posts (Player Profiles) where the title of the specific Club Profile page is also a meta value contained in those Player Profiles against one or two keys: Previous or Later.

The WP query I'm using is:

$clubname = get_the_title();
$args = array(
'post_type'=> 'players',
'meta_key' => 'last_name',  
'orderby' => 'meta_value',
'order'    => 'ASC',
'posts_per_page' => -1,
'meta_query' => array('relation' => 'OR',
   array('key'=> 'previous',
         'value' => $clubname,
         'compare' => 'LIKE',
        ),
   array('key'=> 'later',
         'value' => $clubname,
         'compare' => 'LIKE',
        ),
   ),
$sharedplayers = new WP_Query( $args );

if ($sharedplayers->have_posts()) : while ($sharedplayers->have_posts()) : $sharedplayers->the_post();

This works in the vast majority of cases because most Club Profile titles are unique. There is at least one examples, however, where there's a value that appears within a longer value – i.e., Cove / Coventry City. As such, the list of Player Profiles on the Cove Club Profile is also (incorrectly) including player profiles containing Coventry City as a meta value.

After experimenting with the various compare operators (LIKE, =, and REGEXP), I'm still yet to find a solution where Cove is only identified as a meta_value in its own right, and not confused/conflated with Coventry City. Is it as 'simple' as factoring a space into the query? If so, how is this correctly done where the value is $clubname?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top