最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Rewrite custom post type with taxonomy

matteradmin7PV0评论

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

domain/blog/**name_of_a_city**/title-of-the-article

Until now I have all my links written like this:

domain/blog/title-of-the-article/

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.

domain/barcelona/this-article-title

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() {
    $this->create_cpts_and_taxonomies();
    $this->register_new_terms();
}

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) {
            wp_insert_term(
                $term['name'],
                $this->taxonomy, 
                array(
                    '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

/blog/?article=my_new_article

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!

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

domain/blog/**name_of_a_city**/title-of-the-article

Until now I have all my links written like this:

domain/blog/title-of-the-article/

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.

domain/barcelona/this-article-title

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() {
    $this->create_cpts_and_taxonomies();
    $this->register_new_terms();
}

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) {
            wp_insert_term(
                $term['name'],
                $this->taxonomy, 
                array(
                    '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

/blog/?article=my_new_article

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!

Share Improve this question asked Apr 1, 2019 at 10:54 Amor ArmandAmor Armand 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 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/plugins/custom-post-type-permalinks/

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

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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far