• Resolved swisst

    (@swisst)


    My web is using permalinks “postname”, i.e. https://example.com/sample-post/
    Now I need to add unique number to the end of URL for NEW posts (because I want to be Google News resource).

    Today, I create new post and use “Edit” button and change the permalink (I add number manually to the end).

    Is it possible to change WP behavior in the way, that WP itself suggests permalink in the form https://example.com/sample-post-ID/ ?

    When I change permalinks to “Custom structure” and set it to “/%postname%-%post_id%/”, new permalinks are fine, but function get_permalink returns permalinks in the new structure for all posts. It means, that for old post (without ending ID) get_permalink returns permalink in the new form (with ending ID).

    So what I need is a way, how to modify WP function “suggesting” permalink for new post.

    Any hint?

Viewing 1 replies (of 1 total)
  • Thread Starter swisst

    (@swisst)

    Probably I’ve got it!

    This part of code in functions.php adds post_ID to the slug of post:

    function news_wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) {
    	if ($post_type=='post') $slug = sprintf('%s-%u', $slug, $post_ID);
    	return $slug;
    }
    add_filter('wp_unique_post_slug', 'news_wp_unique_post_slug', 10,6);
Viewing 1 replies (of 1 total)
  • The topic ‘How to add number to permalink’ is closed to new replies.