无法发布自定义类型帖子 - 始终保持草稿状态

时间:2017-03-19 19:13:45

标签: wordpress

我的插件中有自定义帖子类型:

$labels = array(
        'name' => __('Weekday message', 'weekmes'),
        'singular_name' => __('Weekday message', 'weekmes'),
        'add_new' => __('Add New', 'weekmes'),
        'add_new_item' => __('Add New message item', 'weekmes'),
        'edit_item' => __('Edit message item', 'weekmes'),
        'new_item' => __('New message item', 'weekmes'),
        'all_items' => __('All message items', 'weekmes'),
        'view_item' => __('View message item', 'weekmes'),
        'search_items' => __('Search message item', 'weekmes'),
        'not_found' => __('No message item found', 'weekmes'),
        'not_found_in_trash' => __('No message item found in the Trash', 'weekmes'),
        'parent_item_colon' => '',
        'menu_name' => 'Weekday message',
    );
    $args = array(
        'labels' => $labels,
        'description' => 'weekmes custom post type',
        'public' => true,
        'menu_position' => 5,
        'supports' => array('title', 'editor'),
        'has_archive' => true,
    );
    register_post_type('weekmes', $args);

问题是,当我点击puslish时,它会将其保存为草稿。当我尝试编辑它时也保存为草稿。一些清晰的屏幕:

Saving new post

After saving Editing saved post

我也在添加一个元数据,但我认为这不是问题所在:

function weekmesMetaboxContent($post) {
    wp_nonce_field(plugin_basename(__FILE__), 'weekmes_metabox_check_it');
    $post_meta_date = get_post_meta((!empty($post->ID) ? $post->ID : ''),     'weekmes_date', true);
    echo '<label for="weekmes_message_date"></label>';
    echo '<input type="text" id="weekmes_message_date" name="weekmes_date"     value="' . (!empty($post_meta_date) ? $post_meta_date : date('Y-m-d')) . '" placeholder="' . __('Enter appearance date', 'weekmes') . '" readonly/><div class="weekmes-output" style="display:none"></div>';
}

function weekmesMetabox() {
    add_meta_box(
        'weekmes_metabox',
        __('Appearance date', 'weekmes'),
        'weekmesMetaboxContent',
        null,
        'normal',
        'high'
    );
}

add_action('add_meta_boxes', 'weekmesMetabox');

function weekmesPostSave($post_id) {
    if (empty($_POST)) {
        return;
    }
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    if (!wp_verify_nonce($_POST['weekmes_metabox_check_it'],     plugin_basename(__FILE__))) {
        return;
    }
    if ('page' == $_POST['post_type']) {
        if (!current_user_can('edit_page', $post_id)) {
            return;
        }
    } else {
        if (!current_user_can('edit_post', $post_id)) {
            return;
        }
    }

    $message_date = isset($_POST['weekmes_date']) ? $_POST['weekmes_date'] : null;
    update_post_meta($post_id, 'weekmes_date', $message_date);
}

add_action('save_post', 'weekmesPostSave');

有人知道我该怎么办?我知道,我在save_post挂钩时将刚刚保存的帖子的帖子状态更改为“已发布”,但这不是“最佳实践”方式。

0 个答案:

没有答案