jaredatch/Custom-Metaboxes-and-Fields-for-WordPress — Issue #49: Support for selecting post types
Author: zslabs
Created: 2011-12-06 14:12:45+00:00
State: open

Hi,
More of a feature request, but I have previously been using http://www.deluxeblogtips.com/meta-box-script-for-wordpress/ but wanted to give this one a go. One small addition I thought would be nice is the ability, much like the taxonomy select, is to be able to let the user choose items from any post type (pages, posts, etc). What I did with the aforementioned meta box script was added this:

// show pages list
function show_field_pages($field, $meta) {
global $post;

if (!is_array($meta)) $meta = (array) $meta;

$this->show_field_begin($field, $meta);

$options = $field['options'];
$pages = get_pages($options['args']);

// checkbox_list
if ('checkbox_list' == $options['type']) {
foreach ($pages as $page) {
echo "ID, $meta), true, false) . " /> $page->name
";
}
}
// select
else {
echo "";

foreach ($pages as $page) {
echo "
ID, $meta), true, false) . ">$page->post_title
";
}
echo "";
}

$this->show_field_end($field, $meta);
}

// show posts list
function show_field_posts($field, $meta) {
global $post;

if (!is_array($meta)) $meta = (array) $meta;

$this->show_field_begin($field, $meta);

$options = $field['options'];
$posts = get_posts($options['args']);

// checkbox_list
if ('checkbox_list' == $options['type']) {
foreach ($posts as $post) {
echo "ID, $meta), true, false) . " /> $post->name
";
}
}
// select
else {
echo "";

foreach ($posts as $post) {
echo "
ID, $meta), true, false) . ">$post->post_title
";
}
echo "";
}

$this->show_field_end($field, $meta);
}

That basically added in Posts and Pages in array elements to be selected. I know it'll need to be mijiggered to fit in with yours (and maybe make it as simple as specifying the post type in the `meta_boxes[]` array), but for me, the use-case was allowing users to have link options for a Slides content type I created (if they wanted to it to have no link, external link, posts, pages, or category). Thanks!