How To Remove Slug From Custom Post Type In WordPress

How To Remove Slug From Custom Post Type In WordPress
97 Views
<?php
add_filter( 'post_type_link', 'cxc_product_remove_slug', 10, 3 );
function cxc_product_remove_slug( $post_link, $post, $leavename ) {

	// product is my post type
    if ( 'product' != $post->post_type || 'publish' != $post->post_status ) {
        return $post_link;
    }

    $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );

    return $post_link;
}
?>
<?php
add_action( 'pre_get_posts', 'cxc_parse_request_function' );
function cxc_parse_request_function( $query ) {

    if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
        return;
    }

    if ( ! empty( $query->query['name'] ) ) {
        $query->set( 'post_type', 'product' ); // You can add multipe post type like array. ex: array( 'post', 'events', 'page'  )
    }
}
?>

Was this article helpful?
YesNo

Leave a comment

Your email address will not be published.