Loop displaying nothing when dynamically inserting category into query

So I have an array that I’m working with. I’ve typed it out the array so that you can see it. $VideoCategorys

The array will be providing some values like shown here.

<?php $VideoCategorys = array("Stretching Videos", "jumping videos", "running videos"); ?>

These values are WordPress category names that are being entered into sub fields of an ACF repeater

Anyways I need to use these values in a foreach loop like so

                <?php 
						
				    //Now I'd like to do a for loop in which I get the value for each array
					foreach ($VideoCategorys as $Category) {
						
						query_posts('category_name=$Category'); 
                        if ( have_posts() ) : 
						
						echo '<h2>'; echo $Category; echo '<h2>';
						
						while ( have_posts() ) 
						: the_post(); 
                         echo '<p>'; the_title(); echo '<p>';
                        endwhile; 
						
						else: echo '<h2>'; echo 'Nothing in the array'; echo '<h2>';
						
						endif; 
                        wp_reset_query(); 
						
					}
				
				?>    
                <?php wp_reset_query(); ?>

I feel like it’s the above code is right but nothing gets displayed. I double checked it and the WordPress loop is fine. I confirmed it by manually entering the category into the query and it work just fine
<?php query_posts('category_name=Stretching Videos,show_posts=-1'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>

ANY IDEAS OF WHAT I CAN CHANGE?

Single quotes means PHP considers it as a string without changing the variable with its value.
Try double quotes, or concatenate:

query_posts('category_name=' . $Category);

2 Likes

I was using the single quotes because that’s what the syntax shows here https://codex.wordpress.org/Function_Reference/query_posts, but I guess it isn’t necessary? The concatenate worked. I take back everything I said about Darth Vader.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.