Skip to content

Instantly share code, notes, and snippets.

@abachuk
Created July 28, 2012 22:58
Show Gist options
  • Save abachuk/3195113 to your computer and use it in GitHub Desktop.
Save abachuk/3195113 to your computer and use it in GitHub Desktop.
Custom Post type meta box for WordPress (extension to CPT)
/******* RELATED PAGES METABOX *********/
add_action( 'cmb_render_select_post', 'rrh_cmb_render_select_post', 10, 2 );
function rrh_cmb_render_select_post( $field, $meta ) {
global $posts;
$posts = get_posts( array( 'post_type' => 'page', 'posts_per_page' => -1 ));
echo '<select name="', $field['id'], '" id="', $field['id'], '">';
foreach ($posts as $option) {
echo '<option value="', $option->ID, '"', $meta == $option->ID ? ' selected="selected"' : '', '>', $option->post_title, '</option>';
}
echo '</select>';
echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
}
$meta_boxes[] = array(
'id' => 'related_pages',
'title' => 'Related Pages',
'pages' => array('page'), // post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
'fields' => array(
array(
'name' => 'Previous Page',
'desc' => '',
'id' => $prefix . 'prev_page',
'type' => 'select_post',
),
array(
'name' => 'Next Page',
'desc' => '',
'id' => $prefix . 'next_page',
'type' => 'select_post',
),
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment