单击“更新”时如何保存自定义帖子元框

时间:2019-01-08 16:30:31

标签: function advanced-custom-fields

我刚刚设法为我的自定义帖子类型创建了一个自定义metabox,但是在更新帖子时,它又恢复为默认选项。它不会将metabox数据保存到数据库中。

我不确定我的save_post操作中缺少什么。

    //hook to add a meta box
add_action( 'add_meta_boxes', 'featured_meta' );

function featured_meta() {
    //create a custom meta box
    add_meta_box( 'featured-meta', 'Featured This Case Study!', 'featured_rve_function', 'case-studies', 'normal', 'high' );
}

function featured_rve_function( $post ) {

    //retrieve the meta data values if they exist
    $featured_rve_featured = get_post_meta( $post->ID, '_featured_rve_featured', true );

    echo 'Want to add this to the latest works on our homepage?';
    ?>
    <p>Featured: 
    <select name="featured_rve_featured">
        <option value="No" <?php selected( $featured_rve_featured, 'no' ); ?>>Nope, not today</option>
        <option value="Yes" <?php selected( $featured_rve_featured, 'yes' ); ?>>Sure, showcase this beauty!</option>
    </select>
    </p>
    <?php
}

//hook to save the meta box data
add_action( 'save_post', 'featured_rve_save_meta' );
function featured_rve_save_meta( $post_ID ) {
    global $post;
    if( $post->post_type == "case-studies" ) {
        if ( isset( $_POST ) ) {
            update_post_meta( $post_ID, '_featured_rve_featured', strip_tags( $_POST['featured_rve_featured'] ) );
        }
    }
}

0 个答案:

没有答案