Using a variable in "category_and"

techbots
techbots used Ask the Experts™
on
If I use...

query_posts( array( 'category__and' => array( 10,55,56 )));

Open in new window


...I get the expected results.

However I need to use a variable instead:

$catarray = 'array( 10,55,56 )' 
query_posts( array( 'category__and' => $catarray));

Open in new window


When I do that,I don't get any results.

My category IDs (which will populate $catarray) come in from a form and I want only the post that fit ALL of the categories.

Can anyone please help?!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Marco GasiFreelancer
Top Expert 2010

Commented:
The code seems to be correct. How to process query results? That is: what's the line 3?
Dave BaldwinFixer of Problems
Most Valuable Expert 2014
Commented:
Probably remove the quotes because that makes it a string, not an array.
$catarray = array( 10,55,56 ); 
query_posts( array( 'category__and' => $catarray));
 

Open in new window

Marco GasiFreelancer
Top Expert 2010

Commented:
Sure! I've been blind.
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Author

Commented:
Dave, thank you for heading me in the right direction!

I pulled the quotations marks out and though that works with hardcoded values, I still have an issue with building the array as integers rather than a string. Since I have to add the commas when I pull in the category IDs, I am not sure how to do this.

This is what I am using:

foreach ($NV_archivecat as $catlist)
{ 
	$cats = $cats.','.
	(int) $catlist;
}

$catstrimmed = lTrim($cats,',');
$catarray = array($catstimmed);

Open in new window


And that doesn't work.

How do I keep this as integers separated by commas, rather than a string?
Marco GasiFreelancer
Top Expert 2010

Commented:
First, you have to use rTrim to delete the last comma.
Second, usually you can omit the typecasting because Php is smart enough to know when it's dealing with a string or a number.
Thirs, you can rewrite your loop this more concise way:

foreach ($NV_archivecat as $catlist)
{ 
	$.cats = $catlist.',';
}

Open in new window


Maybe it didn't work because of the last comma left there...

Author

Commented:
I was actually building it this way:

				if( !empty($NV_archivecat) )
				{ 
					// Get category ID Array
					$cats = '';
					
					foreach ($NV_archivecat as $catlist)
					{ 
					$cats = $cats.','.$catlist;
					}
				}
				
				$catstrimmed = lTrim($cats,',');
				$catarray = array($catstimmed);

Open in new window


So there is not extra comma.

I do an echo of $catstrimed & get 10,55,56 (as expected)
if I do an echo of $catsarray though, I just get Array

Not sure what that means, but I don't get any results - so the problem must lie somewhere between the two.
Marco GasiFreelancer
Top Expert 2010

Commented:
To print an array you can't use echo, you must use print_r or var_dump.
If you want a more readable output do this

echo "<pre>";
var_dump($catsarray);
echo "</pre>";

Open in new window

Author

Commented:
Thanks! Ok - this is interesting! I think we are finally seeing the problem. I did the var_dump and got this:

array(1) {
  [0]=>
  string(5) "55,56"
}

prinr_r got me this:

Array
(
    [0] => 55,56
)

So, am I reading this right that my array only has one item - the string containing 55,56 -not two items 55 & 56? If so, no wonder it isn't working properly. Of course, I still don't know how to build the array as a variable.
Freelancer
Top Expert 2010
Commented:
Yes, your array hasjust one item... But, sure! I see just now where is the problem!

$catarray = array();
if( !empty($NV_archivecat) )
{ 
    // Get category ID Array
    $cats = '';
    foreach ($NV_archivecat as $catlist)
    { 
        $catarray[] = $catlist;
    }
}
query_posts( array( 'category__and' => $catarray));

Open in new window

Author

Commented:
Holy smokes, Batman, that worked!!!

I can't tell you how happy I am! I would have had to hand code over 300 pages and now I can just pull in the categories from a form. Thank you, thank you, thank you!
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
Marco, you were a little quicker than I was.  Here's my working version... which look an awful lot like Marco's.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Array Cat</title>
</head>
<body>
<h1>Array Cat</h1>
<?php 
$NV_archivecat = array( '12','23','34');
$catsarray = array();
foreach ($NV_archivecat as $catlist)
{ 
	$catsarray[] = (int) $catlist;
}
echo "<pre>";
var_dump($catsarray);
$query = array( 'category__and' => $catsarray);
echo "\r\n";
var_dump($query);
echo "</pre>";

 ?>
</body>
</html>

Open in new window

Author

Commented:
Thanks also to Dave Baldwin for pointing out that problem initially was the array was a string, but the majority of the points go to Marco for helping me find a working solution.
Marco GasiFreelancer
Top Expert 2010

Commented:
@Dave, lol, I wrote less than you!

@techbots: thank you for points. I'm glad to have helped you to avoid such a boring task!
Batman? Mmmhh; where is my Lamborghini?
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
Marco, I often try to write self-contained examples as much to check my own assumptions as anything else.  But I am unlikely to write more than a page unless I already did it for another project.
Marco GasiFreelancer
Top Expert 2010

Commented:
I was only joking, Dave. What I meant was that my quickness was lazyness. It was not a criticism or a way to say you made an error: the ciritic was to me self! :-)

Author

Commented:
Guys like you are the reason I pay for an ongoing subscription to Expert Exchange even though I might only have a question once every few months. I love that I can always count on getting really helpful, timely, friendly solutions!
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
Thank you, @techbots, glad to help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial