0

Hoping someone can help with this...
I've got a php variable called $comma_separated, that is a simple array.

<?php echo $comma_separated; ?>

This gives me as result like this:

'558927', '529306', '529227', '50921', '50923',

I'm trying to use this variable inside a meta_query argument for a WP_Query, like so:

$args = array (
    'post_type'     => 'properties',
    'posts_per_page'  => -1,
    'paged' => $paged,
    'meta_query' => array( 
      array(
        'key' => 'property_code',
        //this works
        //'value' => array('558927', '529306', '529227', '50921', '50923',),
        //this doesn't
        'value' =>  array( $comma_separated ) ,
        'compare' => 'IN', 
        ),
  ),
  $the_query = new WP_Query( $args );

I've tried adding the string as a value manually, which works fine, but I can't get the variable to work. Hopefully this is quit a simple fix, but can't seem to figure it out. Any help would be much appreciated.

titus
  • 11
  • _“that is a simple array”_ - no, it is definitively _not_ an array. If it was, then the attempt to output it via `echo`, would only have gotten you the _word_ `Array`. So it must obviously be a _string_ then. And of course `[1, 2, 3]` and `['1, 2, 3']` are not the same, not even close. You need to use `explode` to _create_ an array out of your string value. – CBroe Jul 20 '21 at 11:22
  • Hi CBroe - thanks for your input. I appreciate the my usage of the expression 'simple array' may well not be correct, but I still don't really understand why the variable doesn't work. If I echo the variable it gives me the _string_ result that I need, but not sure why this doesn't work. Apologies if I seem a bit of a nube about this... – titus Jul 20 '21 at 12:07
  • Because, as I said, `[1, 2, 3]` and `['1, 2, 3']` are two totally different things. And by wrapping `array(…)` around a string variable that contains the text `1, 2, 3`, you are not creating the equivalent of the former, but the latter. – CBroe Jul 20 '21 at 12:22
  • OK, that's helpful, thank you. I've tried creating an array using `$my_array = explode(',', $comma_separated);`, and then using `'value' => array( $my_array ),` in the meta_query, but this still doesn't work. I've also tried `'value' => $my_array,`, but again, no joy. Sorry, but could really do with a little prod in the right direction. Thanks – titus Jul 20 '21 at 12:43
  • If your input value is `'558927', '529306', '529227', '50921', '50923',`, and you explode that just at the comma, then you will get elements like ` '529306'` in between (leading space, not sure if WP is sensitive to that), and even an empty array element at the very end. Try `$val = trim($val, ","); $val = explode(", ", $val);`, that should give you a clean array without any extra whitespace and empty elements. And that _is_ an array already, so don’t wrap it into `array(…)` again. – CBroe Jul 20 '21 at 12:59
  • Ah. That's really helpful, thankyou. The issue in my original string was definitely the leading space. I was actually able to remove that form the original string, which meant I didn't need to use `explode` but that helped to locate the issue, thank you. – titus Jul 20 '21 at 13:07

1 Answers1

0

As CBroe pointed out an array('value, value, value') isn't the same as array('value','value','value'). The first is 1 element, the 2nd are 3 elements.

If you want your string to convert to an array, you have to use explode like this: explode( ',', $comma_separated ). This will convert the string to an array with the separator being the commas;)

Here's some docs: https://www.php.net/manual/en/function.explode.php

  • Thanks for your input your input. The explode trick is useful, so will do some more research on that one... – titus Jul 20 '21 at 13:09