There seems to be a ton of information on checking a meta_key
against an array of meta_values
, but I want to do the opposite.
I have a site with projects, and press reviews. When the client adds a review, they have a custom field (ACF) to select the project it relates to from a list.
I am now trying to display all press reviews that relate to a certain project, together on a page. I have the project ID, and use a query with $args to select the press reviews I want.
My current $args
:
$args = array(
'post_type' => 'pressreview',
'order' => DESC,
'meta_query' => array(
array(
'key' => 'belongs_to_project',
'value' => $projectID,
'compare' => 'LIKE'
)
)
);
The problem is, that custom field 'belongs_to_project
' returns an array with IDs. One press review can be about multiple projects, so I let the client pick multiple projects from the list in that field. A typical array will be like this:
Array ( [0] => 210 [1] => 202 ).
What I want to do, is check whether the $projectID
is in that array.
In the current setup, it sort of works: if the client selected 202 and 210 from the list, and the $projectID
is 210
, the press review is shown. However, if the $projectID = 20
, we get press reviews for projects 201
, 202
, 203
, etc. (since those numbers include '20
', which is how LIKE
works)
Is there any way I can tell this query to only check for 'whole' numbers? So that "2" would only show reviews for project 2
, and not for 201
, 202
, or any other numbers that have a 2 in them?
I've seen there is a REGEXP
option to use instead of LIKE
, but I can't find out how to use it in a way that it checks for only 'whole numbers'.
I've also tried 'IN
', but that doesn't work at all here.
One thing that I thought of, but don't know whether it's possible at all, is to use something like:
'key' => 'belongs_to_project[$i]'
with some kind of loop through the key values, so that $projectID
could be checked with '='
instead of LIKE
against each of the values in the array of the custom field.
I've no idea if there's any such option and how it should be written.
Hope my explanation is clear enough, please let me know if I need to explain better.