1

I have a Custom Field on a page named banner_id_list.

I have a Custom Post Type called top_banner. I add a few banners, note the IDs and then go back to the page and add a comma delimited list of IDs in the banner_id_list custom field.

In my template, the plan is to check the current posts meta using get_post_meta(). That should produce a list of IDs that I can then take and use in get_posts() with post__in = array() (see post__in).

Now on to the code, I'm missing something here but I'm a bit of a newb at this.

// get the banner_id_list based on the meta custom field for this page
$banner_id_list = get_post_meta($post->ID, 'banner_id_list', true);

If I dump that I see the expected data, a list of IDs I entered into the custom field.

Now for my query:

$args = array(
    'post_type' => 'top_banner',
    'post_status' => 'publish',
    'numberposts' => -1,        
    'order' => ASC,
    'orderby' => menu_order,
    'post__in' => array($banner_id_list)
 ); 

 $banners = get_posts($args);

But there's something funky about using $banner_id_list here. It's only getting a single record. But if I manually enter values in place of that variable, it works properly.

I think it's something simple/fundamental I'm missing, hope someone can help!

1 Answer 1

2

If $banner_id_list holds comma-delimited list then to convert it to array instead of

array($banner_id_list)

you need to do

explode(',', $banner_id_list)
1
  • 4
    or wp_parse_id_list (does some extra checks) Jun 24, 2011 at 20:54

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.