Home » Php » php – Hyphen/dash(-) missing in my Custom Post Type URL Slug

php – Hyphen/dash(-) missing in my Custom Post Type URL Slug

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have created a new Custom Post Type(Earnings Transcripts) in WordPress using the below code. The name of my Custom Post Type is “Earnings Transcripts” which is 2 words. So the slug should be “earnings-transcripts”. Instead the URL is “earningstranscripts”. Correct me if I am wrong or if I am missing something here.

function custom_post_type() {

    // Earnings Transcripts 
    $labels = array(
        'name'                => _x( 'Earnings Transcripts', 'Post Type General Name', 'twentysixteen' ),
        'singular_name'       => _x( 'Earnings Transcripts', 'Post Type Singular Name', 'twentysixteen' ),
        'menu_name'           => __( 'Earnings Transcripts', 'twentysixteen' ),
        'parent_item_colon'   => __( 'Parent Earnings Transcripts', 'twentysixteen' ),
        'all_items'           => __( 'All Earnings Transcripts', 'twentysixteen' ),
        'view_item'           => __( 'View Earnings Transcripts', 'twentysixteen' ),
        'add_new_item'        => __( 'Add New Earnings Transcripts', 'twentysixteen' ),
        'add_new'             => __( 'Add New', 'twentysixteen' ),
        'edit_item'           => __( 'Edit Earnings Transcripts', 'twentysixteen' ),
        'update_item'         => __( 'Update Earnings Transcripts', 'twentysixteen' ),
        'search_items'        => __( 'Search Earnings Transcripts', 'twentysixteen' ),
        'not_found'           => __( 'Not Found', 'twentysixteen' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentysixteen' ),
    );
    $args = array(
        'label'               => __( 'Earnings Transcripts', 'twentysixteen' ),
        'description'         => __( 'Earnings Transcripts', 'twentysixteen' ),
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        'taxonomies'          => array( 'Earnings Transcripts' ),
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
    );     
    // Registering your Custom Post Type
    register_post_type( 'Earnings Transcripts', $args );    

}
add_action( 'init', 'custom_post_type', 0 );    

Also while viewing the detailed post under this custom post type, I don’t want this slug to be in the URL before the Post URL.

For example URL of the detailed post under this custom post type should be as follows without the custom post type slug.

www.domainname.com/post-url/

How to&Answers:

You have to change the

// Registering your Custom Post Type
register_post_type( 'Earnings Transcripts', $args );

A change like this:

register_post_type('earnings-transcripts', $args);

The below function will remove the slug from the permalink

 function rf_remove_slug( $post_link, $post, $leavename ) {
  if ( 'earnings-transcripts' != $post->post_type || 'publish' != $post->post_status ) {
    return $post_link;
  }
  $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
  return $post_link;
}
add_filter( 'post_type_link', 'rf_remove_slug', 10, 3 );

The below function will fix the issue of showing the 404 Not found error on the detailed post pages under this custom post type

 function rf_parse_request( $query ) {
  if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
    return;
  }
  if ( ! empty( $query->query['name'] ) ) {
    $query->set( 'post_type', array( 'post', 'earnings-transcripts', 'page' ) );
  }
}
add_action( 'pre_get_posts', 'rf_parse_request' );

Answer:

"rewrite" => array( "slug" => "earnings-transcripts", "with_front" => true ),

Add this parameter in $args, it will work.