Title: foreach() Problem
Last modified: August 19, 2016

---

# foreach() Problem

 *  [GJ](https://wordpress.org/support/users/ttpyro/)
 * (@ttpyro)
 * [17 years, 1 month ago](https://wordpress.org/support/topic/foreach-problem-1/)
 * Hello! I am using this code to generate a simple list of all of the posts in 
   a certain category (based on its slug) by looking at the data in a custom field
   called info. Here is the code:
 *     ```
       <?php
        global $post;
        $slug = get_post_meta(get_the_ID(), "info", true);
        $myposts = get_posts('category_name=' . $slug . '&amp;numberposts=-1');
        foreach($myposts as $post) :?>
       <?php the_title(); ?><br />
       <?php endforeach; ?>
       ```
   
 * This code works but it has some problems with other plugins. The creator of the
   plugin told me, “The code you are using is wrong. foreach() is already depreciated.
   Refer to [http://codex.wordpress.org/Template_Tags/query_posts&#8221](http://codex.wordpress.org/Template_Tags/query_posts&#8221);.
 * I have searched on that link for some time and I am unable to figure out what
   is wrong. I am very new to wordpress and even newer to php. Any idea what I should
   change foreach to?
 * Thanks in advance.

Viewing 1 replies (of 1 total)

 *  [Michael Fields](https://wordpress.org/support/users/mfields/)
 * (@mfields)
 * [17 years, 1 month ago](https://wordpress.org/support/topic/foreach-problem-1/#post-1068474)
 * To my knowledge “foreach” is not and probably never will be depreciated… The 
   following code worked fine in my development server:
 *     ```
       $slug = 'design';
       $myposts = get_posts('category_name=' . $slug . '&amp;numberposts=-1');
       foreach($myposts as $post) {
       	the_title();
       	print '<br />';
       }
       ```
   
 * Which leads me to believe that there is an issue with your call to:
 *     ```
       $slug = get_post_meta( get_the_ID(), "info", true );
       ```
   
 * I _think_ that get_the_id() will only work in or after the Loop, Please try `
   intval( $post->ID )` instead. If that doesn’t work, try printing out the value
   of $slug to the screen.
 * Best of luck,
    -Mike

Viewing 1 replies (of 1 total)

The topic ‘foreach() Problem’ is closed to new replies.

## Tags

 * [code](https://wordpress.org/support/topic-tag/code/)
 * [foreach](https://wordpress.org/support/topic-tag/foreach/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * 1 reply
 * 2 participants
 * Last reply from: [Michael Fields](https://wordpress.org/support/users/mfields/)
 * Last activity: [17 years, 1 month ago](https://wordpress.org/support/topic/foreach-problem-1/#post-1068474)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
