1

I know similar questions were asked before, but this is new account and I can't comment there. This code remove post type slug from permalink. But I have a lot of post type. How can use this code for all of my post types

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

    if ( 'my_custom_post_type' != $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', 'na_remove_slug', 10, 3 );
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( 'my_custom_post_type') );
    }
}
0

 function gp_remove_cpt_slug( $post_link, $post ) {
        if ( 'race' === $post->post_type && 'publish' === $post->post_status ) {
            $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
        }
        return $post_link;
    }
    add_filter( 'post_type_link', 'gp_remove_cpt_slug', 10, 2 );

At this point, trying to view the link would result in a 404 (Page Not Found) error. That’s because WordPress only knows that Posts and Pages can have URLs like domain.com/post-name/ or domain.com/page-name/. We need to teach it that our custom post type’s posts can also have URLs like domain.com/cpt-post-name/.

<?php
/**
 * Have WordPress match postname to any of our public post types (post, page, race).
 * All of our public post types can have /post-name/ as the slug, so they need to be unique across all posts.
 * By default, WordPress only accounts for posts and pages where the slug is /post-name/.
 *
 * @param $query The current query.
 */
function gp_add_cpt_post_names_to_main_query( $query ) {
    // Bail if this is not the main query.
    if ( ! $query->is_main_query() ) {
        return;
    }
    // Bail if this query doesn't match our very specific rewrite rule.
    if ( ! isset( $query->query['page'] ) || 2 !== count( $query->query ) ) {
        return;
    }
    // Bail if we're not querying based on the post name.
    if ( empty( $query->query['name'] ) ) {
        return;
    }
    // Add CPT to the list of post types WP will include when it queries based on the post name.
    $query->set( 'post_type', array( 'post', 'page', 'race' ) );
}
add_action( 'pre_get_posts', 'gp_add_cpt_post_names_to_main_query' );

That’s it! Just change both instances of race in these code samples to the slug of your custom post type, and replace gp_ with whatever function prefix you’d like (your initials would be fine), and you should be all set. Going to Settings > Permalinks and saving the permalink structure to end in /%postname%/ may also be necessary.

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.