如何在帖子URL中使用自定义元字段而不是CPT标题

时间:2018-08-29 作者:Mona Coder

我有一个自定义的帖子类型,它没有使用任何WP内置支持,如(\'标题\'、\'编辑器\'、\'作者\'、\'缩略图\'、\'摘录\'、\'注释\')。相反,我使用自己的元数据库来发布CPT。现在,在创建CPT并发布之后,当我单击查看帖子时,它会导航到以auto-draft 在第二次CPT上auto-draft-2 等等

http://domain.com/movies/auto-draft/

正如您所注意到的,这仍然是导航到发布默认标题,而不是发布自定义元框。你能告诉我如何更新这个以使用特定的metabox吗$name

$we_movieMetas = get_post_custom( $post->ID );
$name = isset( $we_movieMetas[\'name_box\'] ) ? esc_attr( $we_movieMetas[\'name_box\'][0] ) : \'\';
$boxOffice = isset( $we_movieMetas[\'boxOffice_box\'] ) ? esc_attr( $we_movieMetas[\'boxOffice_box\'][0] ) : \'\';

Update

这是代码

function cpt_movie_post() {

    $labels = array(
        \'name\'                 => \'Movies\',
        \'singular_name\'        => \'Movie\',
        \'menu_name\'            => \'Movie\',
        \'name_admin_bar\'       => \'Movie\',
        \'parent_item_colon\'    => \'Parent Movie\',
        \'all_items\'            => \'All Movie\',
        \'view_item\'            => \'View Women \',
        \'add_new_item\'         => \'Add New Women \',
        \'add_new\'              => \'Add New Women \',
        \'new_item\'             => \'New Movie\',
        \'edit_item\'            => \'Edit Moviee Item\',
        \'update_item\'          => \'Update Movie Item\',
        \'search_items\'         => \'Search Movie Item\',
        \'not_found\'            => \'Moviee Not found\',
        \'not_found_in_trash\'   => \'Moviee Not found in Trash\',
    );
    $args = array(
        \'label\'                => \'Movie\',
        \'description\'          => \'This Post Type Adds Movie to Website\',
        \'labels\'               => $labels,
        \'supports\'             => array(\'\'),
        \'taxonomies\'           => array( ),
        \'hierarchical\'         => true,
        \'public\'               => true,
        \'show_ui\'              => true,
        \'show_in_menu\'         => true,
        \'show_in_nav_menus\'    => true,
        \'show_in_admin_bar\'    => true,
        \'menu_position\'        => 5,
        \'menu_icon\'            => \'dashicons-layout\',
        \'rewrite\'              => array( \'slug\' => \'movie-post\', \'with_front\' => false ),
        \'can_export\'           => true,
        \'has_archive\'          => true,
        \'exclude_from_search\'  => false,
        \'publicly_queryable\'   => true,
        \'capability_type\'      => \'post\',
    );
    register_post_type( \'movie_post\', $args );

}

add_action( \'init\', \'cpt_movie_post\', 0 );


function movie_mtbox()
{
$post_types = array ( \'movie_post\');
 foreach( $post_types as $post_type ){
    add_meta_box(
        "product-detail",
        "Movie Details ",
        "render_movie_metas",
        $post_type,
        "normal",
        "high"
    );
    }

}
function render_movie_metas( $post )
{
$we_movieMetas = get_post_custom( $post->ID );
$name = isset( $we_movieMetas[\'name_box\'] ) ? esc_attr( $we_movieMetas[\'name_box\'][0] ) : \'\';
$boxOffice = isset( $we_movieMetas[\'boxOffice_box\'] ) ? esc_attr( $we_movieMetas[\'boxOffice_box\'][0] ) : \'\';
}

add_action( \'save_post\', \'we_metas_save\' );
function we_metas_save( $post_id )
{
    if( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) return;
    if( !isset( $_POST[\'meta_box_nonce\'] ) || !wp_verify_nonce( $_POST[\'meta_box_nonce\'], \'my_meta_box_nonce\' ) ) return;
    if( !current_user_can( \'edit_post\' ) ) return;
    $allowed = array( 
        \'a\' => array( // on allow a tags
            \'href\' => array() // and those anchords can only have href attribute
        )
    );

    // Probably a good idea to make sure your data is set
    if( isset( $_POST[\'name_box\'] ) ){
        update_post_meta( $post_id, \'name_box\', wp_kses( $_POST[\'name_box\'], $allowed ) );
    }
    if( isset( $_POST[\'boxOffice_box\'] ) ){
        update_post_meta( $post_id, \'boxOffice_box\', wp_kses( $_POST[\'boxOffice_box\'], $allowed ) );
    }

}

1 个回复
SO网友:Farsad

你必须使用wp_update_post 函数,并根据中的可用字段更新所需字段WP_Post

add_action( \'save_post\', \'wpse75679_save_post\' );
function wpse75679_save_post( $post_id )
{
    if( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) return;

    // verify post is not a revision
    if ( ! wp_is_post_revision( $post_id ) ) {

    // unhook this function to prevent infinite loop
    remove_action( \'save_post\', \'wpse75679_save_post\' );

    // update the post slug and title
    wp_update_post( array(
        \'ID\' => $post_id,
        \'post_title\' => \'new-title\'
        \'post_name\' => \'new-slug\'
    ));

    // re-hook this function
    add_action( \'save_post\', \'wpse75679_save_post\' );
}

结束

相关推荐

如何在编辑或添加帖子屏幕上添加Metabox来拉取自定义帖子列表(任意两个)?

我想将metabox添加到自定义帖子列表中,并在编辑或添加帖子屏幕上选择任意两个。最后,当文章发布时,将其显示在单个文章页面上。请帮帮我!任何帮助都将不胜感激。。。。 add_action( \'add_meta_boxes\', function () { add_meta_box( \'yourcustom_sectionid\', __( \' Custom Offer Section\', \'yourtextdomain\'