WordPress – changing a custom post type archive link

I had to create a custom post type, let’s call it ‘trucks’. The link to the archive of the posts looks like that: http://example.com/wpdirectory/?post_type=trucks
I want it to look like that: http://example.com/wpdirectory/trucks without the ‘?post_type=’ query. How can I affect it?

Related posts

Leave a Reply

2 comments

  1. Try using the add_rewrite_rule() function is your functions.php file. Try this:

    add_action( 'init', 'rewritePostType' );
    function rewritePostType(){
        add_rewrite_rule('^wpdirectory/([^/]*)/?','index.php?post_type=$matches[1]','top');
        add_rewrite_tag('%post_type%','([^&]+)');
    }
    

    Otherwise you can overwrite the permastructures in Settings -> Permalinks in WordPress and hard code it in.