php - Hyphen missing in my Custom Post Type URL Slug

52

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". What am I missing 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 );    
340

Answer

Solution:

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

Answer

Solution:

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

Add this parameter in$args, it will work.

315

Answer

Solution:

Bit late. I hope this one will be helpful for others who face this scenario.

When it comes to Custom Post types, Slugs should always have underscores instead of hyphens as there are various places in WordPress core that utilize the slugs, and that would break if not using an underscore.

You can check this article for more details on this topic: https://premium.wpmudev.org/forums/topic/why-is-underscore-permitted-in-the-post-type-but-dash-is-disallowed/#post-505820

People are also looking for solutions to the problem: php - Seo Friendly URL with Rewrite

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.