Title: Custom field Query Posts
Last modified: August 19, 2016

---

# Custom field Query Posts

 *  Resolved [Tom Morton](https://wordpress.org/support/users/tm3909/)
 * (@tm3909)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/custom-field-query-posts/)
 * A client needs a “related” posts where you manually enter in the related posts
   via a custom field.
 * I’ve decided to insert the related posts ID’s into a custom field, like so: 13,267.
 * Then, using a custom query in the sidebar, I’m trying to use post__in in the 
   query to call the numbers in the custom field. Thus far, it either breaks or 
   only shows the first number.
 * Help! I’ve tried it about 45,000 different ways, all resulting in nothing. Any
   suggestions would be greatly appreciated!
 * Heres my code:
 *     ```
       <h2>Related Posts</h2> 
   
       	<?php $related = get_post_meta($post->ID, "related", $single=true);
       		echo $related; //To check if it works
       		$args = array_merge( array('post__in'  => array($related), $wp_query->query ) );
   
       		query_posts($args);
       		if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
   
       		<div id="<?php the_ID(); ?>">
       			<a href="<?php the_permalink();?>"><p class="caption"><?php the_title(); ?></p></a>
       		</div>
   
       	<?php endwhile; else: ?>
        	<p>no related</p>
       	<?php endif; wp_reset_query();?>
       ```
   

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

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/custom-field-query-posts/#post-1424521)
 * your custom field contains a string, not an array:
    first step is to explode 
   this string to get the array; then remove the word ‘array’ from the $args list:
 * this following seems to work in my local test installation:
 *     ```
       <h2>Related Posts</h2> 
   
       	<?php $related = get_post_meta($post->ID, "related", $single=true);
       		echo $related; //To check if it works
       		$related=explode(',',$related);
       		$args = array_merge( array('post__in'  => $related, $wp_query->query ) );
   
       		query_posts($args);
       		if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
   
       		<div id="<?php the_ID(); ?>">
       			<a href="<?php the_permalink();?>"><p class="caption"><?php the_title(); ?></p></a>
       		</div>
   
       	<?php endwhile; else: ?>
        	<p>no related</p>
       	<?php endif; wp_reset_query();?>
       ```
   
 *  Thread Starter [Tom Morton](https://wordpress.org/support/users/tm3909/)
 * (@tm3909)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/custom-field-query-posts/#post-1424556)
 * [@alchymyth](https://wordpress.org/support/users/alchymyth/) Thank you so much!
   Case solved!

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

The topic ‘Custom field Query Posts’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [Tom Morton](https://wordpress.org/support/users/tm3909/)
 * Last activity: [16 years, 3 months ago](https://wordpress.org/support/topic/custom-field-query-posts/#post-1424556)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
