Okay, so I have this confusing problem with a string. I'm using a loop to construct the string, and I use print_r($exclude)
to see the result. The output of the string is:
101,102,135
... which is correct. Then I'm trying to use $exclude
in an argument-array for a WordPress-query:
'terms' => array($exclude),
In short this should exclude posts from the categories with ID's mentioned above. But this does not work as intended. Unless if I write the numbers directly like this, it works:
'terms' => array(101,102,135),
So what is the difference between the $exclude
-string and writing the numbers manually...!?
When you do this
'terms' => array($exclude)
Your terms
array looks like this:
Array
(
[0] => 101,102,135
)
Solution
'terms' => explode(',', $exclude)
$exclude
becomes
Array
(
[0] => 101
[1] => 102
[2] => 135
)
You are assigning a string to the 0-th key in array, whereas you should be passing array elements.
explode will split up your string into an array based on a passed delimiter
(in your case a comma).
You need to use explode function of php.
Try below code :
'terms' => explode(',', $exclude)
Refer this link for more details - http://www.w3schools.com/php/func_string_explode.asp
In your case if you print_r
array($exclude)
it will give
Array
(
[0] => 101,102,135
)
what you need is
Array
(
[0] => 101,
[1] => 102,
[2] => 135
)
ie, an array with those numbers as array elements so just do 'terms' => explode(',', $exclude)
and you will be good to go... http://php.net/manual/en/function.explode.php