• 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)
  • 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

    (@greencode)

    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”

    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.