3

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.

2 Answers 2

6

The solution was that I need to compare the literal value of $projectID, so that LIKE compares an exact string instead of just numbers. To make $projectID literal, it needs to be wrapped in quotes.

So, I changed this line:

'value' => $projectID,

to:

'value' => '"'.$projectID.'"',

Which solves the problem.

1
  • Very good. This works because the values are stored between quotes inside the array in the db. Great simple solution. Thanks!
    – Ben
    Feb 7, 2018 at 19:25
0

I believe that you should use IN at compare but you said this's not working. and I notice something (please forgive me if this's a mistake ): you write example for array

Array ( [0] => 210 [1] => 202 ).

this mean the value in array is NUMERIC and the Default type is 'CHAR'. so you can try

$args = array(
    'post_type'     =>  'pressreview',
    'order'         =>  DESC,
    'meta_query' => array(
         array(
            'key' => 'belongs_to_project',
            'value' => $projectID,
            'compare'   => 'IN',
            'type'     => 'NUMERIC'
         )
     )
);
3
  • I don't think I can use '=', because $projectID is not an array, so it can't be equal. I agree with adding 'type' => 'NUMERIC', but for some reason that makes the query not return any results at all. Even so, I've tried your exact solution as well to be sure, but it didn't work. I get zero results when using NUMERIC.
    – Els
    Sep 20, 2015 at 20:22
  • My mistake :/ , I write = instead of IN , ok. now you need to var_dump returned array of belongs_to_project without any compare to be sure what kind of array values return. Sep 22, 2015 at 13:40
  • 1
    I know exactly the array it returns. and it's numeric. However, I've found the solution (with outside help!), will post the answer in a few minutes.
    – Els
    Sep 22, 2015 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.