On my WP blog my goal is to rewrite urls like:
domain.com/blog/**name_of_a_city**/title-of-the-article
Until now I have all my links written like this:
domain.com/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.com/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!