• turkision

    (@turkision)


    Hi everyone,

    I am using a theme which utilizes different post types such as regular “posts”, “videos” and “tv series”. I can add a post at “posts” tab, I can add a video(this is a kind of post) at “videos” tab, and the same goes for “tv series” tab too.

    I am experiencing an annoying url problem. My permalinks are custom structured: /%category%/%postname%/
    This works perfectly fine when I post a normal post via “posts” tab. However, when I post a custom post type (in videos or tv series tab) the url goes something like: mydomain..com/title/example-movie-name and when I post an episode, the url goes: mydomain..com/episodes-title/example-series-episode-3/. /title/ is always there for “video” posts, and /episodes-title/ is alawys there for “tv series” posts. They are so irritating.

    I tried using custom code via code snippet plugin, thanks to chatgpt, to remove those unnecessary slugs (/title/ and /episodes-title/) from the url. It actually worked but it also broke all the links and the whole website so I had to undo it. I also tried inserting that code in functions.php but the results were the same. Also, my mod_rewrite is on.

    There are more than 2000 “tv series posts(episodes)” and 100 “video posts”, so even if there is a way to do this with a plugin, manually one by one, I guess I couldn’t do it as it’d take soo long. I need to remove those unnecessary slugs in a shorter way.

    At this point, I am not even sure if I really need to remove that slug. I just think it is unnecessary and in terms of seo, it’s better if I remove it. The cleaner the url is, the better experience for the user, right? I am not sure even if I manage to edit the urls, would it create indexing problems?

    Here is the custom code chatgpt gave me: (video_skrn and episodes_skrn are the names for my custom post types)

    function custom_post_type_slug($args, $post_type) {
    if ($post_type === ‘video_skrn’) { // Replace ‘videos’ with your post type slug
    $args[‘rewrite’] = array(‘slug’ => ‘/’); // Blank slug removes it completely
    }
    if ($post_type === ‘episodes_skrn’) { // Replace with your post type slug
    $args[‘rewrite’] = array(‘slug’ => ‘/’);
    }
    return $args;
    }
    add_filter(‘register_post_type_args’, ‘custom_post_type_slug’, 10, 2);

    I appreciate any help, thanks!

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Kevin McClellan

    (@crazywpgeek)

    WordPress always “expects” a slug for a custom post type. If you remove the slug completely, and you have a page or post in another post type (such as posts or pages), or post in same category name which has the same slug for the content (ie… post-title or page-title), then it would conflict and you would most likely face issues on those pages due to those sharing the same full URL, which could be what the issue was with critical errors – though I wouldn’t imagine the whole site going down.

    If you remove the slug, you would need to add custom rewrite rules.

    I did a little research for you and it looks like, for the video post type that this might work (please note that I have not tested this as I don’t have access to the theme). Please take a backup or know how to undo this if you have problems.

    function change_post_type_slug() {
    $args = array(
    'rewrite' => array( 'slug' => '/' ), // Attempts to remove the slug
    // other post type arguments
    );
    register_post_type( 'video_skrn', $args );
    }
    add_action( 'init', 'change_post_type_slug' );

    function custom_rewrite_rules() {
    add_rewrite_rule('([^/]+)$', 'index.php?video_skrn=$matches[1]', 'top');
    }
    add_action('init', 'custom_rewrite_rules');

    My suggestion would be to instead just change the slug to a different slug that makes more sense for your website. For example, to change that slug to just /video/, then you would want to use this code instead.

    function change_post_type_slug() {
    $args = array(
    'rewrite' => array( 'slug' => 'video' ), // Sets the slug to /video/
    // other post type arguments
    );
    register_post_type( 'video_skrn', $args );
    }
    add_action( 'init', 'change_post_type_slug' );

    If you need to do this multiple times, make sure to change the function name. So instead of change_post_type_slug() you could think about using change_video_skrn_slug()

    I hope this helps or at least gets you in the right direction.

    Most importantly, have a super awesome weekend.

    Kevin

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.