Remove POST_TYPE from custom post type permalink

As @WebElaine commented on this question I tried the following code which worked for me.

First, remove the slug from the permalink

function na_remove_slug( $post_link, $post, $leavename ) {

  if ( 'services' != $post->post_type || 'publish' != $post->post_status ) {
      return $post_link;
  }

  $post_link = str_replace( "https://wordpress.stackexchange.com/" . $post->post_type . "https://wordpress.stackexchange.com/", "https://wordpress.stackexchange.com/", $post_link );

  return $post_link;
}

add_filter( 'post_type_link', 'na_remove_slug', 10, 3 );

Just removing the slug isn’t enough. Right now, you’ll get a 404 page because WordPress only expects posts and pages to behave this way. You’ll also need to add the following:

function na_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', 'services', 'page' ) );
  }
}

add_action( 'pre_get_posts', 'na_parse_request' );

Then refresh the permalink.

Leave a Comment