add_action('init', 'foo_articles');
function foo_articles() {
$labels = array(
'name' => _x('Foo Articles', 'post type general name', 'your-plugin-textdomain'),
'singular_name' => _x('Foo Articles', 'post type singular name', 'your-plugin-textdomain'),
'menu_name' => _x('Foo Articles', 'admin menu', 'your-plugin-textdomain'),
'name_admin_bar' => _x('My Slug', 'add new on admin bar', 'your-plugin-textdomain'),
'add_new' => _x('Add New', 'book', 'your-plugin-textdomain'),
'add_new_item' => __('Add New Book', 'your-plugin-textdomain'),
'new_item' => __('New Book', 'your-plugin-textdomain'),
'edit_item' => __('Edit Book', 'your-plugin-textdomain'),
'view_item' => __('View Book', 'your-plugin-textdomain'),
'all_items' => __('All Books', 'your-plugin-textdomain'),
'search_items' => __('Search Books', 'your-plugin-textdomain'),
'parent_item_colon' => __('Parent Books:', 'your-plugin-textdomain'),
'not_found' => __('No books found.', 'your-plugin-textdomain'),
'not_found_in_trash' => __('No books found in Trash.', 'your-plugin-textdomain'),
);
$args = array(
'labels' => $labels,
'description' => __('Description.', 'your-plugin-textdomain'),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'my_slug','with_front'=>true),
'hierarchical' => true,
'menu_position' => null,
'show_in_rest' => true,
'rest_base' => 'my_slug',
'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments','page-attributes'),
);
register_post_type('my_slug', $args);
flush_rewrite_rules(true);
}
add_action('init', 'my_book_taxonomy');
function my_book_taxonomy() {
$labels = array(
'name' => _x('Foo Taxonomy', 'taxonomy general name'),
'singular_name' => _x('Foo Taxonomy', 'taxonomy singular name'),
'search_items' => __('Search Genres'),
'all_items' => __('All Genres'),
'parent_item' => __('Parent Genre'),
'parent_item_colon' => __('Parent Genre:'),
'edit_item' => __('Edit Genre'),
'update_item' => __('Update Genre'),
'add_new_item' => __('Add New Genre'),
'new_item_name' => __('New Genre Name'),
'menu_name' => __('Foo Taxonomy'),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array('slug' => 'my_slug', 'with_front' => TRUE),
'show_in_rest' => true,
'rest_base' => 'foo_taxonomy',
);
register_taxonomy('foo_taxonomy', array('my_slug'), $args);
flush_rewrite_rules(true);
}
function taxonomy_slug_rewrite() {
global $wp_rewrite;
$rules = array();
$taxonomies = get_taxonomies(array('_builtin' => false), 'objects');
$post_types = get_post_types(array('public' => true, '_builtin' => false), 'names');
foreach ($post_types as $post_type) {
$post_type_data = get_post_type_object($post_type);
$post_type_slug = $post_type_data->rewrite['slug'];
foreach ($taxonomies as $taxonomy) {
if ($taxonomy->object_type[0] == $post_type_slug) {
$categories = get_categories(array('type' => $post_type_slug, 'taxonomy' => $taxonomy->name, 'hide_empty' => 0));
/* @var $category type */
foreach ($categories as $category) {
$rules[$post_type_slug . '/' . $category->slug . '/?$'] = 'index.php?' . $category->taxonomy . '=' . $category->slug;
}
}
}
}
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter( 'generate_rewrite_rules', 'taxonomy_slug_rewrite' );
add_action('init', 'foo_articles');
function foo_articles() {
$labels = array(
'name' => _x('Foo Articles', 'post type general name', 'your-plugin-textdomain'),
'singular_name' => _x('Foo Articles', 'post type singular name', 'your-plugin-textdomain'),
'menu_name' => _x('Foo Articles', 'admin menu', 'your-plugin-textdomain'),
'name_admin_bar' => _x('My Slug', 'add new on admin bar', 'your-plugin-textdomain'),
'add_new' => _x('Add New', 'book', 'your-plugin-textdomain'),
'add_new_item' => __('Add New Book', 'your-plugin-textdomain'),
'new_item' => __('New Book', 'your-plugin-textdomain'),
'edit_item' => __('Edit Book', 'your-plugin-textdomain'),
'view_item' => __('View Book', 'your-plugin-textdomain'),
'all_items' => __('All Books', 'your-plugin-textdomain'),
'search_items' => __('Search Books', 'your-plugin-textdomain'),
'parent_item_colon' => __('Parent Books:', 'your-plugin-textdomain'),
'not_found' => __('No books found.', 'your-plugin-textdomain'),
'not_found_in_trash' => __('No books found in Trash.', 'your-plugin-textdomain'),
);
$args = array(
'labels' => $labels,
'description' => __('Description.', 'your-plugin-textdomain'),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'my_slug','with_front'=>true),
'hierarchical' => true,
'menu_position' => null,
'show_in_rest' => true,
'rest_base' => 'my_slug',
'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments','page-attributes'),
);
register_post_type('my_slug', $args);
flush_rewrite_rules(true);
}
add_action('init', 'my_book_taxonomy');
function my_book_taxonomy() {
$labels = array(
'name' => _x('Foo Taxonomy', 'taxonomy general name'),
'singular_name' => _x('Foo Taxonomy', 'taxonomy singular name'),
'search_items' => __('Search Genres'),
'all_items' => __('All Genres'),
'parent_item' => __('Parent Genre'),
'parent_item_colon' => __('Parent Genre:'),
'edit_item' => __('Edit Genre'),
'update_item' => __('Update Genre'),
'add_new_item' => __('Add New Genre'),
'new_item_name' => __('New Genre Name'),
'menu_name' => __('Foo Taxonomy'),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array('slug' => 'my_slug', 'with_front' => TRUE),
'show_in_rest' => true,
'rest_base' => 'foo_taxonomy',
);
register_taxonomy('foo_taxonomy', array('my_slug'), $args);
flush_rewrite_rules(true);
}
function taxonomy_slug_rewrite() {
global $wp_rewrite;
$rules = array();
$taxonomies = get_taxonomies(array('_builtin' => false), 'objects');
$post_types = get_post_types(array('public' => true, '_builtin' => false), 'names');
foreach ($post_types as $post_type) {
$post_type_data = get_post_type_object($post_type);
$post_type_slug = $post_type_data->rewrite['slug'];
foreach ($taxonomies as $taxonomy) {
if ($taxonomy->object_type[0] == $post_type_slug) {
$categories = get_categories(array('type' => $post_type_slug, 'taxonomy' => $taxonomy->name, 'hide_empty' => 0));
/* @var $category type */
foreach ($categories as $category) {
$rules[$post_type_slug . '/' . $category->slug . '/?$'] = 'index.php?' . $category->taxonomy . '=' . $category->slug;
}
}
}
}
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter( 'generate_rewrite_rules', 'taxonomy_slug_rewrite' );