I'm using a custom meta box to output a list of post IDs within a particular post type ("Publications" in this example). Is there a way I can output this array into a custom loop to only show posts with those ID's?

This is the code I am using;

Meta box array

$related = get_post_meta( get_the_ID(), 'ps_related-publications', false );
foreach ( $related as $meta );

WP_Query loop

$related_publication_widget = new WP_Query( array (
    'post_type' => 'publications',
    'p' => $meta, 
    'posts_per_page' => '-1',
));while ( $related_publication_widget->have_posts() ) : 

Because the ID's are output as just numbers (Ie; without the ',' required to separate ID's in a query, it's only showing the latest post as oppose to all of them as defined in the loop.

Any ideas?


You're looking for post__in & post__not_in parameters http://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters

Both of them are an array of ID's for the post's to include or exclude in the results

BTW in your code, you had a semicolon just after your foreach

  • Well.... that's embarrasing. Thanks for that. Let this be a lesson to everyone. Get your sleep!! – Dean Elliott Oct 25 '12 at 18:33
  • Actually, all that's done is list ALL the posts in that post type... The ID's are being output like this (If the post ID's were 23, 35 and 48) 233548. How can I separate them? – Dean Elliott Oct 25 '12 at 18:35
  • You'll have to add the commas when saving the post meta. Trying to separate them here is impossible. If the ID's were 233 & 548, it would also result in the same string – Mridul Aggarwal Oct 25 '12 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.