On my WP blog my goal is to rewrite urls like:


Until now I have all my links written like this:


Since I don't want to rewrite these existing articles url and change completely all the existing works, I decided to create a new custom post type named "Articles". I've been reading and trying many ways to do so, but I'm having some difficulties.

I thought the best way was to create taxonomies, since I will be adding many "taxonomy" (cities) as I write my blog. For example, if I write an article about Barcelona I would like to be able to add a city as a taxonomy and to see it withing my url.


Here is my codes:

I've been through the first stages:

-> Creation of a custom post type "Articles"

function cptui_register_my_cpts_article() {

 * Post Type: Articles.

$labels = array(
    "name" => __( "Articles", "rowling" ),
    "singular_name" => __( "Article", "rowling" ),

$args = array(
    "label" => __( "Articles", "rowling" ),
    "labels" => $labels,
    "description" => "",
    "public" => true,
    "publicly_queryable" => true,
    "show_ui" => true,
    "delete_with_user" => false,
    "show_in_rest" => true,
    "rest_base" => "",
    "rest_controller_class" => "WP_REST_Posts_Controller",
    "has_archive" => "cities",
    "show_in_menu" => true,
    "show_in_nav_menus" => true,
    "exclude_from_search" => false,
    "capability_type" => "post",
    "map_meta_cap" => true,
    "hierarchical" => false,
    "rewrite" => false,
    "query_var" => true,
    "supports" => array( "title", "editor", "thumbnail", "author" ),
    "taxonomies" => array( "category", "post_tag" ),

register_post_type( "article", $args );

add_action( 'init', 'cptui_register_my_cpts_article' );

-> I made a custom taxonomy through a plug-in with 2 pre-registrered options (city1 & city2)

class Test_Terms {

function __construct() {
    register_activation_hook( __FILE__,array( $this,'activate' ) );
    add_action( 'init', array( $this, 'create_cpts_and_taxonomies' ) );

function activate() {

function create_cpts_and_taxonomies() {

    $args = array( 
        'hierarchical'                      => true,  
        'labels' => array(
            'name'                          => _x('Cities', 'taxonomy general name' ),
            'singular_name'                 => _x('City', 'taxonomy singular name'),
            'search_items'                  => __('Search city'),
            'popular_items'                 => __('Popular city'),
            'all_items'                     => __('All city'),
            'edit_item'                     => __('Edit city'),
            'edit_item'                     => __('Edit city'),
            'update_item'                   => __('Update city'),
            'add_new_item'                  => __('Add new city'),
            'new_item_name'                 => __('New city'),
            'separate_items_with_commas'    => __('Seperate city with commas'),
            'add_or_remove_items'           => __('Add or remove city'),
            'choose_from_most_used'         => __('Choose from most used city')
        'query_var'                         => true,  
        'rewrite'                           => array('slug' =>'city', 'with_front' =>false, 'hierarchical'=>true),        
        'capabilities' => array(
    'manage_terms'  => 'update_plugins',
    'edit_terms'    => 'update_plugins',
    'delete_terms'  => 'update_plugins',
    'assign_terms'  => 'edit_posts'
    register_taxonomy( 'city', array( 'article' ), $args );

function register_new_terms() {
    $this->taxonomy = 'city';
    $this->terms = array (
        '0' => array (
            'name'          => 'city1',
            'slug'          => 'city1',
            'description'   => 'for city1',
        '1' => array (
            'name'          => 'city2',
            'slug'          => 'city2',
            'description'   => 'for city2',

    foreach ( $this->terms as $term_key=>$term) {
                    'description'   => $term['description'],
                    'slug'          => $term['slug'],
        unset( $term ); 

$Test_Terms = new Test_Terms();

-> Now I can create a "new article" from the admin and register a taxonomy but I want this taxonomy to be included in the url and to have the /name_of_the_artle/ type of permalinks instead of "?". For now I have


I've been trying to follow this way and this post but I haven't been successful. And one issue as well is that I don't want to rewrite my existing posts but only the ones created under my new custom post type "Articles".

Thank you in advance for all your advices, you could save me!

1 Answer 1


Regarding the code base rewrites, you should be using %city% instead of city, as custom taxonomy used in slug param for rewrites should be enclosed in %TAXONOMY%

WordPress should still be able to redirect your old posts from the old URLs to the new ones. Did you add the filter on the post_type_link?

If you're doing this for a custom post type, you can use this plugin to customize the permalink in the core WordPress permalink configuration area: https://wordpress.org/plugins/custom-post-type-permalinks/

There's also this plugin: https://wordpress.org/plugins/permalinks-customizer/

Which allows you to use custom taxonomies in your permalink structure tags.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.