Avatar of techbots
techbots
Flag for United States of America asked on

Using a variable in "category_and"

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?!
WordPressPHP

Avatar of undefined
Last Comment
Dave Baldwin

8/22/2022 - Mon
Marco Gasi

The code seems to be correct. How to process query results? That is: what's the line 3?
SOLUTION
Dave Baldwin

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Marco Gasi

Sure! I've been blind.
techbots

ASKER
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?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Marco Gasi

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...
techbots

ASKER
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 Gasi

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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
techbots

ASKER
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.
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
techbots

ASKER
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 Baldwin

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

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
techbots

ASKER
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 Gasi

@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 Baldwin

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Marco Gasi

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! :-)
techbots

ASKER
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 Baldwin

Thank you, @techbots, glad to help.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck