Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I was wondering how i can remove the prefix from the slugs of custom post types using htaccess rules or any other appropriate method.

Currently my custom post types look like this:

http://www.domain.com/os_estate/5-bedroom-property-for-sale

and i would simply like this to be:

http://www.domain.com/5-bedroom-property-for-sale

I was able to achieve this via a plugin but it significantly added to the page load time..

My common settings in Permalinks is set to "Post Name"

share|improve this question
add comment

2 Answers

// Well then, first use a function to mention your custom post types.

function my_custom_post_types(){
    return array(
        'os_estate', 
        'os_villas', 
        'os_chalets', 
        'os_cottages'
    );
}

// Then, filtering the request.

    add_action( 'parse_request','my_custom_post_types_parse_request', 999);

function my_custom_post_types_parse_request( $query ){

    if( isset($query->request) )
    {
        $req = explode('/', ltrim($query->request,'/'));
        if( !is_admin() && isset($req['0']) )
        {
            global $wpdb;
            $name = stripslashes($req['0']);
            $_post = $wpdb->get_row( "SELECT * FROM $wpdb->posts WHERE post_status = 'publish' AND post_name ='". $name ."' AND post_type IN =('" . join("', '", my_custom_post_types() ) . "')" );

            if( $_post )
            {
                $query->query_vars['error'] = '';
                $query->query_vars['post_type'] = $_post->post_type;
                $query->query_vars['name'] = $name;
            }
        }
    }

// And next filter all the desired post types link.

add_filter( 'post_type_link', 'my_custom_post_types_permalink', 10, 2);

function my_custom_post_types_permalink( $post_link, $post ){
    if( in_array( $post->post_type, my_custom_post_types() ) && !empty($post->post_name) && $post->post_status == 'publish' && '' != get_option('permalink_structure') )
    {
        $post_name = $post->post_name;
        $post_link = home_url("/$post_name/");
    }
    return $post_link;
}
share|improve this answer
    
I actually have a lot of custom post types namely os_estate, os_villas, os_chalets, os_cottages - could you please modify your code to include all of them? –  Joanna Mikalai Dec 9 '13 at 23:12
    
thank you i will test this tomorrow and will let you know how it goes.. I also did notice that i could modify my theme directly.. is your solution in terms of performance and speed adequate? –  Joanna Mikalai Dec 10 '13 at 0:40
    
Exactly, you can see i am just using one query to find a match within defined custom post types. But if you use empty slug, that will either return 404 error, or if that returns fine, then post (default post type) page will be inaccessible. But with this solution, we leaving the rest of your site as it is. Thanks. –  Shazzad Dec 10 '13 at 22:01
add comment

I think that you just need to put rewrite rule in your code in place where you register post type.

Just place this code

$rewrite = array(
    'slug'                => '',
    'with_front'          => true,
    'pages'               => true,
    'feeds'               => true,
    );

after the $labels array();

and before $args = array();

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.