Title: Query posts with custom field value
Last modified: August 21, 2016

---

# Query posts with custom field value

 *  [greencode](https://wordpress.org/support/users/greencode/)
 * (@greencode)
 * [12 years, 12 months ago](https://wordpress.org/support/topic/query-posts-with-custom-field-value/)
 * I have the following query
 *     ```
       <ul>
       <?php
         $featuredPosts = new WP_Query( array(
         	'post_type' => 'events',
         	'post__in' => array(110)
         ) );
       while ($featuredPosts->have_posts()) : $featuredPosts->the_post(); ?>
       <li>
       <?php the_title() ?>
       </li>
       <?php endwhile; ?>
       </ul>
       ```
   
 * which works well but what I would like to do is add the “post__in” contents to
   a custom field on the post and to add those results to the query. I’ve tried
 *     ```
       <?php
         $featuredPosts = new WP_Query( array(
         	'post_type' => 'events',
         	'post__in' => array(get_post_meta($post->ID, 'custom_field_name', true))
         ) );
       while ($featuredPosts->have_posts()) : $featuredPosts->the_post(); ?>
       ```
   
 * But this doesn’t return any results.
 * Any help or ideas would be greatly appreciated.

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

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [12 years, 12 months ago](https://wordpress.org/support/topic/query-posts-with-custom-field-value/#post-3757470)
 * Please explain a bit more.
 * Is the custom field on a Page using a template containing the code you showed?
 * Is the content of the CF a single post ID?
 *  Thread Starter [greencode](https://wordpress.org/support/users/greencode/)
 * (@greencode)
 * [12 years, 12 months ago](https://wordpress.org/support/topic/query-posts-with-custom-field-value/#post-3757472)
 * Hi, the code above is in a page template. In the customs field would be a series
   of post ids such as “112,243,34”
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [12 years, 12 months ago](https://wordpress.org/support/topic/query-posts-with-custom-field-value/#post-3757473)
 * get_post_meta() with the ‘true’ option returns a single string, so if there are
   possibly multiple comma-separated values in the field, you need to explode them
   into an array:
 *     ```
       'post__in' => explode(',', get_post_meta($post->ID, 'custom_field_name', true)))
         ) );
       ```
   

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

The topic ‘Query posts with custom field value’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 2 participants
 * Last reply from: [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * Last activity: [12 years, 12 months ago](https://wordpress.org/support/topic/query-posts-with-custom-field-value/#post-3757473)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
