Я создаю пользовательский пост типа названия курсов. Для этого пользовательского типа записи создайте динамический c раскрывающийся список данных метабокса, но не сохраняйте в БД. Пожалуйста, проверьте мой код, где я делаю что-то не так, чтобы сохранить данные. Мой мета-бокс показывает нормально, но данные я ввод не сохраняется. Т.е. когда я нажимаю «обновить» в сообщении, мета-поле возвращается к моему тексту-заполнителю.
if ( ! function_exists('add_courses') ) {
// Register Custom Post Type
function add_courses() {
$labels = array(
'name' => _x( 'Courses', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Course', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Courses', 'text_domain' ),
'name_admin_bar' => __( 'Courses', 'text_domain' ),
'archives' => __( 'Course Archives', 'text_domain' ),
'attributes' => __( 'Course Attributes', 'text_domain' ),
'parent_item_colon' => __( 'Parent Course:', 'text_domain' ),
'all_items' => __( 'All Courses', 'text_domain' ),
'add_new_item' => __( 'Add New Course', 'text_domain' ),
'add_new' => __( 'Add New Course', 'text_domain' ),
'new_item' => __( 'New Course', 'text_domain' ),
'edit_item' => __( 'Edit Course', 'text_domain' ),
'update_item' => __( 'Update Course', 'text_domain' ),
'view_item' => __( 'View Course', 'text_domain' ),
'view_items' => __( 'View Courses', 'text_domain' ),
'search_items' => __( 'Search Course', 'text_domain' ),
'not_found' => __( 'Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ),
'featured_image' => __( 'Featured Image', 'text_domain' ),
'set_featured_image' => __( 'Set featured image', 'text_domain' ),
'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
'use_featured_image' => __( 'Use as featured image', 'text_domain' ),
'insert_into_item' => __( 'Insert into item', 'text_domain' ),
'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
'items_list' => __( 'Courses list', 'text_domain' ),
'items_list_navigation' => __( 'Courses list navigation', 'text_domain' ),
'filter_items_list' => __( 'Filter Courses list', 'text_domain' ),
);
$args = array(
'label' => __( 'Course', 'text_domain' ),
'description' => __( 'Add Courses', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', 'comments', 'trackbacks',
'revisions', 'custom-fields', 'page-attributes', 'post-formats' ),
'taxonomies' => array('post_tag'),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
'show_in_rest' => true,
'register_meta_box_cb' => 'wpt_add_faculty_metaboxes',
);
register_post_type( 'course_type', $args );
// register taxonomy
register_taxonomy('courses', 'course_type', array('hierarchical' => true, 'label' => 'Course Categories', 'query_var' => true, 'rewrite' => array( 'slug' => 'course-type' )));
}
add_action( 'init', 'add_courses', 0 );
function wpt_add_faculty_metaboxes() {
add_meta_box(
'wpt_faculty_member',
'Faculty',
'wpt_faculty_member',
'course_type',
'side',
'default'
);
}
add_action( 'add_meta_boxes', 'wpt_add_faculty_metaboxes' );
function wpt_faculty_member() {
global $post;
wp_nonce_field( basename( __FILE__ ), 'event_fields' );
$faculty_roles = get_post_custom( $post->ID );
$args = array(
'role' => 'faculty',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$faculties = get_users( $args );
echo '
<select name="faculty_role" id="faculty_role">
<option value="">Select Faculty...
</option>';
foreach ( $faculties as $faculty ) :
echo '
<option value="' . $faculty->ID . '">' . $faculty->user_login . '
</option>';
endforeach;
echo '
</select>';
}
/**
* Save the metabox data
*/
add_action( 'save_post', 'wpt_save_events_meta', 1, 2 );
function wpt_save_events_meta( $post_id, $post ) {
// Return if the user doesn't have edit permissions.
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
// Verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times.
if ( ! isset( $_POST['faculty_role'] ) || ! wp_verify_nonce( $_POST['event_fields'], basename(__FILE__) ) ) {
return $post_id;
}
// Now that we're authenticated, time to save the data.
// This sanitizes the data from the field and saves it into an array $events_meta.
$events_meta['faculty_role'] = esc_textarea( $_POST['faculty_role'] );
// Cycle through the $events_meta array.
// Note, in this example we just have one item, but this is helpful if you have multiple.
foreach ( $events_meta as $key => $value ) :
// Don't store custom data twice
if ( 'revision' === $post->post_type ) {
return;
}
if ( get_post_meta( $post_id, $key, false ) ) {
// If the custom field already has a value, update it.
update_post_meta( $post_id, $key, $value );
} else {
// If the custom field doesn't have a value, add it.
add_post_meta( $post_id, $key, $value);
}
if ( ! $value ) {
// Delete the meta key if there's no value
delete_post_meta( $post_id, $key );
}
endforeach;
}
}