Problem with url incrementation (post-type-1-1-1)

Forums Forums Remove CPT base Problem with url incrementation (post-type-1-1-1)

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #204
    grumo64
    Participant

    Hi there,
    I need help with something I don’t understand.

    I’ve got a Page with the name “Tarifs” and the slug is mydomain.com/tarifs

    I’ve got a few CPT with these structure :
    city1/tarifs
    city2/tarifs
    city3/tarifs…

    The problem is my cpt slug are not :
    mydomain.com/city1/tarifs
    but mydomain.com/city1/tarifs-1 (and same thing for city2, city3…)

    And one more thing, very problematic, is everytime I update my CPT the slug add -1 until 5 times, like this
    mydomain.com/city1/tarifs-1-1-1-1-1

    Temporarily I commented : add_filter( ‘wp_unique_post_slug’, array( $this, ‘fix_slug’ ), 100, 6 );
    and it works but I would prefer a real Dev solution 😉

    Thanks for your help
    Regards

    #206
    kubiq
    Keymaster

    Hello,

    every post should have unique post_name, this is how WordPress works.
    Of course, there are many ways how to get around it, but it depends on specific setup.
    Maximum what I can do is to create checkbox for disabling unique slugs, but it can cause many problems for many users.

    The thing is, that in general, when you need so many same post_names, you’re doing something wrong. For example in your case I recommend to use custom endpoint instead of creating subpost for every post.

    https://codex.wordpress.org/Rewrite_API/add_rewrite_endpoint

    Put this in you functions.php

    add_filter( 'query_vars', 'add_query_vars' );
    function add_query_vars( $vars ){
    	$vars[] = 'tarifs';
    	return $vars;
    }
    
    add_rewrite_endpoint( 'tarifs', EP_PERMALINK );

    then in single-city.php use $wp_query->query['tarifs']

    #209
    grumo64
    Participant

    Sorry for the delay.
    Thank you for your support.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.