Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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

share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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