0

I'm triyng to put a server rule on a htaccess to remove some parts of the urls that wordpress generates via permalinks when you work with CPT (custom post types). The fact is that I'm not sure that this can work via a simple Rewrite rule.

my urls actually looks like this:

http://www.13mdn.com/newreleases_post/white-stripes.

I would like to change to:

http://www.13mdn.com/white-stripes

and

http://www.13mdn.com/oldreleases_post/the-who

I would like to change to:

http://www.13mdn.com/the-who

here is the rules I made on htacces file

RewriteRule ^newreleases_post/(.*)$ $1
RewriteRule ^oldreleases_post/(.*)$ $1

This don't work.

I also Have tried puting the rule via wordpress function like this

Community
  • 1
  • 1
Frnnd Sgz
  • 236
  • 1
  • 4
  • 19

2 Answers2

4

To remove custom post type slug from post URLs, you need to add the following in your functions.php:

function remove_cpt_slug($post_link, $post, $leavename) {
    if($post->post_type == 'newreleases_post' || $post->post_type == 'oldreleases_post') {
        $post_link = str_replace('/'.$post->post_type.'/', '/', $post_link);
    }
    return $post_link;
}
add_filter('post_type_link', 'remove_cpt_slug', 10, 3);

This will change all the URL for your CPTs to remove the CPT name. To let Wordpress know that he should check for those CPT, add the following function:

function request_cpt($query) {
    if(! $query->is_main_query()) {
        return;
    }
    if(count($query->query) != 2 || ! isset( $query->query['page'])) {
        return;
    }
    if (! empty( $query->query['name'])) {
        $query->set('post_type', array('post', 'newreleases_post', 'oldreleases_post'));
    }
}
add_action('pre_get_posts', 'request_cpt');

Last step, to redirect your old URLs to the new one, add this on the top of your htaccess (before the Wordpress part):

RewriteEngine On
RewriteRule ^newreleases_post/(.*)$ $1 [L,R=301]
RewriteRule ^oldreleases_post/(.*)$ $1 [L,R=301]

Please note that this could end up to conflicts if you use the same permalink for a CPT and a page.

vard
  • 4,057
  • 2
  • 26
  • 46
0

Simply change your permalink to a custom format : /%postname%/

John Slegers
  • 45,213
  • 22
  • 199
  • 169
ehmad11
  • 1,395
  • 3
  • 14
  • 29
  • I said before that I have set the permalinks with custom structure, but wordpress puts an slug with the custom post type name on every post...that's the point – Frnnd Sgz Dec 22 '15 at 11:07
  • 2
    @FrnndSgz You missed an important part of information here: it's that `newreleases_post` and `oldreleases_post` are CPT. Both ehmad and me assumed that it was categories. – vard Dec 22 '15 at 11:09
  • 1
    my bad, question wasn't clear. can you please undone the downvote, – ehmad11 Dec 22 '15 at 11:42