Title: Loop inside single post based on custom field values
Last modified: August 19, 2016

---

# Loop inside single post based on custom field values

 *  [mistercyril](https://wordpress.org/support/users/mistercyril/)
 * (@mistercyril)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/loop-inside-single-post-based-on-custom-field-values/)
 * Hello,
 * I am trying to display a list of posts at the bottom of my single.php template,
   that are defined by a custom field.
 * **The idea is to use a post’s custom field to set a few post “id”s that would
   be used to populate the list of posts.**
 * I can get a loop working but its not using the post ids I defined. It seems to
   be ignoring them all together.
 * Could anyone give me a quick example of what my query should look like?
 * The **custom field Key is “Products”** and the values should be ids separated
   by commas.
 * Thanks in advance,
    C.

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

 *  [Tom Morton](https://wordpress.org/support/users/tm3909/)
 * (@tm3909)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/loop-inside-single-post-based-on-custom-field-values/#post-2024595)
 * Funny you mention this, I did a similar thing not with posts but with pages and
   a custom theme menu. I think this might work:
 *     ```
       <?php 
   
           $single_posts = echo get_post_meta($post->ID, 'Products', true);
   
           $post_array = array($single_posts); //construct array for single posts id's
   
       	$args = array('order' => 'desc', 'p' => $post_array);
           $single_post_fields = new WP_Query($args); 
   
           $count = 0; ?>
           <div role="complementary" class="homepage-widgets" id="footer-widget-area">
   
       	<?php while ($single_post_fields->have_posts()) : $single_post_fields->the_post();
           $count++;
           $do_not_duplicate = $post->ID; ?>
   
       		<p>Whatever your HTML and WP stuff is to call the post (the_title, the_content, etc)</p>
   
           <?php endwhile; wp_reset_query(); ?>
       ```
   
 * I haven’t tried it, but its similar to my page code so it should work.
 *  Thread Starter [mistercyril](https://wordpress.org/support/users/mistercyril/)
 * (@mistercyril)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/loop-inside-single-post-based-on-custom-field-values/#post-2024646)
 * Hey, Thanks for your code.
 * I tried it out but I can’t get it to work entirely.
 * FYI : I have a comma separated list of ids in my “Product” field (i.e. “1, 56,
   98, 203”).
    - If I test the **$single_posts** variable it lists the post ids as it should.
    - If I test the **$post_array variable** it outputs “array” instead of the values
      of the array.
 * Here are my findings so far. Do you have any ideas?
 * **Here’s the non working version**
 *     ```
       <?php $products = get_post_meta($post->ID, 'products', true);
       			if($products != '') {
       			$single_posts = get_post_meta($post->ID, 'products', true);
       			$post_array = array($single_posts); //construct array for single posts id's
       			$args = array(
       				'order' => 'desc',
       				'p' => $post_array
       			);
       			$single_post_fields = new WP_Query($args);
       			$count = 0; ?>
                   	<div class="categoryimages">
                       <?php echo $args ?>
                           <ul>
                   	<?php while ($single_post_fields->have_posts()) : $single_post_fields->the_post(); $do_not_duplicate = $post->ID; ?>
                       		<li><?php the_post_thumbnail('thumbnail'); ?></li>
           			<?php endwhile; wp_reset_query(); ?>
                           </ul>
                       </div>
   
                   <?php } ?>
       ```
   
 * **This version displays the first post in the list of ids (better, but I need
   it to display all of them)**
 *     ```
       <?php $products = get_post_meta($post->ID, 'products', true);
       			if($products != '') {
       			$single_posts = get_post_meta($post->ID, 'products', true);
       			$post_array = array($single_posts); //construct array for single posts id's
       			$args = array(
       				'order' => 'desc',
       				'p' => $single_posts
       			);
       			$single_post_fields = new WP_Query($args);
       			$count = 0; ?>
                   	<div class="categoryimages">
                       <?php echo $args ?>
                           <ul>
                   	<?php while ($single_post_fields->have_posts()) : $single_post_fields->the_post(); $do_not_duplicate = $post->ID; ?>
                       		<li><?php the_post_thumbnail('thumbnail'); ?></li>
           			<?php endwhile; wp_reset_query(); ?>
                           </ul>
                       </div>
   
                   <?php } ?>
       ```
   

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

The topic ‘Loop inside single post based on custom field values’ is closed to new
replies.

## Tags

 * [custom field](https://wordpress.org/support/topic-tag/custom-field/)
 * [id](https://wordpress.org/support/topic-tag/id/)
 * [post](https://wordpress.org/support/topic-tag/post/)
 * [query](https://wordpress.org/support/topic-tag/query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [mistercyril](https://wordpress.org/support/users/mistercyril/)
 * Last activity: [15 years, 1 month ago](https://wordpress.org/support/topic/loop-inside-single-post-based-on-custom-field-values/#post-2024646)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
