WordPress.org

Forums

Changing Custom Post Permalink Structure (1 post)

  1. magicwings
    Member
    Posted 1 year ago #

    Hi all,

    I'm running a theme which uses custom post types. However, I wish to change the permalinks to my posts to include the PostID.

    I have added this code:

    add_action('init', 'movies_rewrite');
    function movies_rewrite() {
        global $wp_rewrite;
        $wp_rewrite->add_permastruct('movies', 'Movies-detail/%postname%-%post_id%', true, 1);
        add_rewrite_rule('movies/([0-9]{4})/(.+)/?$', 'index.php?movies=$matches[2]', 'top');
        $wp_rewrite->flush_rules(); // !!!
    
    }

    ...and it works!

    If I go to http://www.example.com/Movies-detail/examplepost-1234/, it comes up with the correct post, and everything's updated.

    Well... not quite.

    All of the links on every page still point to the old permalinks, resulting in a 404 error: they're not updated.

    I've gone to the Settings>>Permalinks page and saved the settings, changed them and re-saved, cleared my server cache, my local cache, everything, and I'm finally led to assume it's a problem in the code - or a line or two I've missed.

    How can I update all the permalinks on my site to direct to the new structure?

    Thanks in advance

Topic Closed

This topic has been closed to new replies.

About this Topic