0

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() ) : 
$related_publication_widget->the_post();

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?

1 Answer 1

1

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

3
  • Well.... that's embarrasing. Thanks for that. Let this be a lesson to everyone. Get your sleep!! Oct 25, 2012 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? Oct 25, 2012 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 Oct 25, 2012 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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