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.

Hey Guys, I'm using a meta box to store a bunch of page ID's. When I try to pass the page ID's to the post__in parameter of WP_Query it doesn't work becuase the metabox comes throug as a string, when it needs to be a comma separated integers.

So, say the "relatedpages" meta box contains: 55, 33, 22

$relatedpages = get_post_meta($post->ID, 'relatedpages', true);

$args = array(
    'post_type'         => 'page',
    'posts_per_page'    => -1,
    'order'         => 'ASC',
    'orderby'       => 'menu_order',
    'post__in'      => array($relatedpages)
    );

$myposts = get_posts($args);
echo $myposts;

The problem is that $related pages is now "55, 33, 22" rather than 55, 33, 22

How can I overcome this? Is there a way to store just the integers in the meta box, rather than converting them to a string?

Thanks, Drew

share|improve this question
    
By the way, to store ids, you're using a custom field. To input those ids you're using a custom metabox. –  scribu Apr 10 '11 at 2:05
    
Hey scribu, am I just using the wrong words in my question, or should I be storing my ID's differently? –  Drew Baker Apr 18 '11 at 17:25
    
Well, since you asked: scribu.net/wordpress/posts-to-posts –  scribu Apr 18 '11 at 18:03

2 Answers 2

up vote 2 down vote accepted
'post__in'      => explode( ',', $relatedpages )
share|improve this answer
    
Cast to int whilst we're there? array_map( 'absint', explode( ',', $relatedpages ) ) ... –  t31os Apr 10 '11 at 9:37
    
That's done anyway in WP_Query. –  scribu Apr 10 '11 at 14:25

Thanks for the help, but in the end I think the problem was the 'post__in' parameter can't be used in get_posts like I wanted. As per the codex for get_posts, 'include' is the way to do it.

$relatedpages = get_post_meta($post->ID, 'relatedpages', true);

$args = array(
    'post_type'             => 'page',
    'posts_per_page'        => -1,
    'order'             => 'ASC',
    'orderby'           => 'menu_order',
    'include'           => $relatedpages
    );

$myposts = get_posts($args);

Thanks anyway!

share|improve this answer

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.