0

I have a custom Post Type which is not using any of WP builtin supports like( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ). Instead I am using my own metaboxes to publish the CPT. Now after creating the CPT and publishing when I click on the View Post it navigates to a URL ending with auto-draft and on second CPT to auto-draft-2 and so on

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

as you noticed this is still navigating to post default title instead of Post Custom Meta box. Can you please let me know how I can update this to use an specific metabox , lets say $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

Here is the code

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 ) );
    }

}
  • Are you creating the posts programmatically or in the editor? If programmatically, it might be simplest to copy your custom field into the title so WP automatically knows what to set as the permalink. You could also try custom rewrite rules: wpquestions.com/Add_custom_field_to_permalink_URL/… – WebElaine Aug 29 '18 at 21:38
  • Thanks, can you please let me know how I can Copy the custom field to Title? – Mona Coder Aug 29 '18 at 21:45
  • Pass it into wp_insert_post() as the post_title. If you can share the code you're using to create the posts it will be easier to plug that in so you have the full solution. – WebElaine Aug 29 '18 at 21:51
  • Sure I will update the post – Mona Coder Aug 29 '18 at 21:57
  • @WebElaine, I just updated the post can you please let me know where can I use wp_insert_post() here? Thanks – Mona Coder Aug 29 '18 at 22:04
1

You have to use a wp_update_post function and update the required fields based on available fields in WP_Post class.

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' );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.