0

The variable:

$products_in_cart = '112,109,106';

The query_posts:

query_posts(array( 
'post_type' => 'product',
'post__not_in' => array($products_in_cart),
...

If I replace in the query $products_in_cart by 112,109,106 it work.

The variable is ok outside the loop, can’t understand what is wrong with this basic use, thanks for your help.

1
  • My guess is that array(112,96,80) is Array( [0] => 112 [1] => 96 [2] => 80), and array('112,96,80') is Array( [0] => 112,96,80), so when you put it like a string it sees the value as a string, instead of each value being separate (due to quotes). Commented Nov 4, 2015 at 14:01

2 Answers 2

2

You have to pass an array of IDs to post__not_in. If you start with a comma delimited string you can use the PHP function explode to expand it to an array:

...
'post__not_in' => explode(",", $products_in_cart),
...
Sign up to request clarification or add additional context in comments.

Comments

1

Because you need an array on 'post__not_in' and you are creating an array but with one input: '112, 109, 106'. You have to use the explode function. Something like this:

$products_in_cart = '112,109,106';

And then:

'post__not_in' => explode(",", $products_in_cart),

Or just create an array from the beginning:

$products_in_cart = array(112, 109, 106);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.