Title: Variable on query_posts isn&#039;t working
Last modified: August 30, 2016

---

# Variable on query_posts isn't working

 *  Resolved [Famke__](https://wordpress.org/support/users/famke_/)
 * (@famke_)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/variable-on-query_posts-isnt-working/)
 * Hey guys,
 * I’m working on a little project which requires me to do something based on the
   category name of a post. The whole thing is working exactly as it should… but
   when I look at the site, I get an error message: `Notice: Undefined variable:
   cat_name`. Note that I do get the results I need, but there still is the message.
 * If anyone knows what I’m doing wrong, that would be great.
 * This is the code:
 *     ```
       <?php
       	query_posts('category_name=zoekennaar' . $cat_name);
       	$content = get_post_field('post_content', $cat_name);
       	$content = explode('|', $content);
       		foreach ($content as &$zoekenNaar)
       		$zoekenNaar = explode(':',$zoekenNaar);
       		echo '<pre>';
       			print_r($content);
       		echo '</pre>';
       ?>
       ```
   

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [stephencottontail](https://wordpress.org/support/users/stephencottontail/)
 * (@stephencottontail)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/variable-on-query_posts-isnt-working/#post-6496297)
 * How are you defining `$cat_name`? What is the output of `var_dump( $cat_name )`?
 *  Thread Starter [Famke__](https://wordpress.org/support/users/famke_/)
 * (@famke_)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/variable-on-query_posts-isnt-working/#post-6496400)
 * Actually, I thought I defined `$cat_name` in the first line of the code: figured
   the string of `$cat_name` would now be the category name I gave it. Apparently
   that’s not true, the output of var_dump gives me ‘null’.
 * I initially wrote this code with a post_id instead of category_name, but I’m 
   making this for someone who needs to be able to make changes. Therefore, I need
   category_name rather than post_id. With post_id it worked like this, no errors
   whatsoever:
 *     ```
       <?php
       	$postid = 33;
       	$content = get_post_field('post_content', $postid);
       	$content = explode('|', $content);
       		foreach ($content as &$zoekenNaar)	{
       		$zoekenNaar = explode(':',$zoekenNaar);
       			echo '<pre>';
       			print_r($zoekenNaar);
       			echo '</pre>';
       		}
       ?>
       ```
   
 * I have tried just changing it like that, adding `$category_name = 'zoekennaar'`,
   but it doesn’t work.
 * (I’ve added brackets to the foreach loop as you can see, forget that in the first
   place, and added print_r $zoekenNaar instead of $content now, which is what should
   be happening).
 * ADDED:
 * I created a new post to see what would happen. My code as it is now shows the
   most recent post, so `$cat_name` indeed doesn’t work. Figured most of you already
   know, but I’m not the only newbie so it might be useful for someone 🙂
 *  [stephencottontail](https://wordpress.org/support/users/stephencottontail/)
 * (@stephencottontail)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/variable-on-query_posts-isnt-working/#post-6496403)
 * As you’ve seen, you need to pass a post ID as the second argument to `get_post_field()`
   and nothing else will work.
 * One way to get the IDs would be to use [`get_posts()`](https://codex.wordpress.org/Template_Tags/get_posts):
 * `<?php $posts = get_posts( array( 'category_name' => $cat_name ) ); ?>`
 * `$posts` would then be an array of objects and you could iterate over the array
   to get the post content.
 * What exactly are you trying to do?
 *  Thread Starter [Famke__](https://wordpress.org/support/users/famke_/)
 * (@famke_)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/variable-on-query_posts-isnt-working/#post-6496417)
 * Thank you so much for the quick responds and help!
 * The get_posts you send, is that a line of code I put in instead of these lines?
 *     ```
       query_posts('category_name=zoekennaar' . $cat_name);
       $content = get_post_field('post_content', $cat_name);
       ```
   
 * To be honest, I don’t have much an idea of what I’m doing. I’m totally new to
   this and someone explained this to me, so I thought I’d give it a shot.
 * First of, my website needs to be editable by my boss, without her having to dig
   into PHP files.
 * I’m basically trying to generate a list from a post. This list offers the users
   four options. The user can click one of those options, and another list of options
   will apear.
 * Every list is a new post with a new category name. As said above, my boss needs
   to be able to add to or take things away from the list, without having to go 
   into php.
 *  [stephencottontail](https://wordpress.org/support/users/stephencottontail/)
 * (@stephencottontail)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/variable-on-query_posts-isnt-working/#post-6496459)
 * Yeah. You’d have to define $cat_name beforehand, probably by whatever method 
   you were using before. You’d then use something like `foreach()` to iterate over
   the array of posts returned by `get_posts()`:
 *     ```
       <?php
         $posts = get_posts( array( 'category_name' => $cat_name ) );
         foreach( $posts as $post ) {
           $content = get_post_field( 'post_content', $post->ID );
           $content = explode( '|', $content );
           foreach ( $content as &$zoekenNaar ) {
             $zoekenNaar = explode( ':', $zoekenNaar);
             echo '<pre>';
             print_r( $zoekenNaar );
             echo '</pre>';
           }
         }
       ?>
       ```
   
 *  Thread Starter [Famke__](https://wordpress.org/support/users/famke_/)
 * (@famke_)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/variable-on-query_posts-isnt-working/#post-6496469)
 * I copypasted your code and then decided to replace $cat_name with the actual 
   category_name, and that worked. This is exactly what I was trying to do in the
   first place, so thanks! 🙂

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Variable on query_posts isn't working’ is closed to new replies.

## Tags

 * [category_name](https://wordpress.org/support/topic-tag/category_name/)
 * [query_posts](https://wordpress.org/support/topic-tag/query_posts/)
 * [variable](https://wordpress.org/support/topic-tag/variable/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 2 participants
 * Last reply from: [Famke__](https://wordpress.org/support/users/famke_/)
 * Last activity: [10 years, 9 months ago](https://wordpress.org/support/topic/variable-on-query_posts-isnt-working/#post-6496469)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
