I have a custom post type, and in there I have a metabox with a dropdown. The dropdown is populated with the titles of posts from another custom post type.
My metabox system works. I tried hardcoding the options and they save correctly. I can also pull the data from the custom post type to populate the dropdown.
The issue lies within the save/loading of data.
I've tried wp_query and get_posts, with their corresponding resets, but whenever I try to pull in the data, the dropdown isn't updated and the permalink is changed to the last pulled custom post type.
I just need to know how to pull in the data correctly, or reset the loop properly.
/* Create role metabox */
function create_roles_metabox() {
global $post;
add_meta_box(
'roles_metabox',
'Role Area & Level',
'show_roles_metabox',
'role',
'side',
'high'
);
}
/* Show roles metabox */
function show_roles_metabox() {
global $post;
wp_nonce_field(basename(__FILE__), 'role_nonce');
$pro_areas = array(
'One',
'Two',
'Three'
);
/*
$args = array(
'post_type' => 'proarea',
'publish_status' => 'publish',
'posts_per_page' => -1,
'order' => 'ASC'
);
$the_query = new WP_Query($args);
if($the_query->have_posts()) {
echo '<ul>';
while ( $the_query->have_posts()) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
}
*/
/*
$mcpt_query = array();
$the_query = get_posts('post_type=proarea');
foreach ( $the_query as $post ) : setup_postdata( $post );
$mcpt_query[] = array(
'title' => get_the_title($post->ID)
);
endforeach;
wp_reset_postdata();
print_r($mcpt_query);
*/
echo '<span><label for="pro_area">Professional Area: </label></span>';
echo '<select name="pro_area">';
foreach($pro_areas as $pro_area) {
if($pro_area == get_post_meta($post->ID, 'pro_area', true))
echo '<option selected>'. $pro_area .'</option>';
else
echo '<option>'. $pro_area .'</option>';
}
echo '</select>';
}
/* Save roles metabox */
function save_roles_metabox($post_id, $post) {
if(!isset($_POST['role_nonce']) || !wp_verify_nonce($_POST['role_nonce'], basename(__FILE__)))
return $post->ID;
if(!current_user_can('edit_post', $post->ID))
return $post->ID;
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post->ID;
if(isset($_POST['pro_area']))
update_post_meta($post_id, 'pro_area', $_POST['pro_area']);
}
add_action('add_meta_boxes', 'create_roles_metabox');
add_action('save_post', 'save_roles_metabox', 1, 2);