[wp-hackers] RFC: On solving WordPress automatically adding -2, -3 to the permalink

Ryan Chan ryanchan404 at gmail.com
Sun Apr 29 10:00:01 UTC 2012


WordPress does not allow post_name to have the same value, even we
have used the explicit ID as the permalink format -
/products/{post_name}/{post_ID}

e.g.

What we want:
http://www.example.com/products/foo/1
http://www.example.com/products/foo/2

What we get currently:
http://www.example.com/products/foo/1
http://www.example.com/products/foo-2/2


To solve this, we have write a plugin:
================================


        add_filter('post_type_link', function($link, $post = 0) {

            if ($post->post_type == 'product')
            {
                $postName = sanitize_title($post->post_title, $post->post_name);

                return home_url(sprintf('products/%s/%d', $postName,
$post->ID));
            }
            else
            {
                return $link;
            }
        }, 1, 3)
;
================================


Seems to be working, any comment and drawback I havn't think of?

Thanks.


More information about the wp-hackers mailing list