Wordpress CPT multiple categories in the url - wordpress

I have a CPT Traffi coffice.
Url: www.domain.com/traffic-offices/
When add a new like www.domain.com/traffic-offices/germany/ its ok.
When i add a new post Saksen then it automaticly makes this url www.domain.com/traffic-offices/saksen but i want www.domain.com/traffic-offices/germany/saksen. If i fill that in it automaticly makes www.domain.com/traffic-offices/germany-saksen.
How can i achieve this url www.domain.com/traffic-offices/germany/saksen ?
function trafficoffice_post_type() {
// Labels
$labels = array(
'name' => _x("trafficoffices", "post type general name"),
'singular_name' => _x("trafficoffices", "post type singular name"),
'menu_name' => 'trafficoffices',
'add_new' => _x("Add new", "trafficoffice item"),
'add_new_item' => __("Add new"),
'edit_item' => __("trafficoffice aanpassen"),
'new_item' => __("Add new trafficoffice"),
'view_item' => __("View trafficoffice"),
'search_items' => __("Search Profiles"),
'not_found' => __("No Profiles Found"),
'not_found_in_trash' => __("No Profiles Found in Trash"),
'parent_item_colon' => ''
);
// Register post type
register_post_type('trafficoffice', array(
'labels' => $labels,
'public' => true,
'has_archive' => false,
'rewrite' => array(
'slug' => 'traffic-offices',
),
'supports' => array('title', 'editor', 'thumbnail')
));
}
add_action('init', 'trafficoffice_post_type', 0);

One way of doing that is using taxonomies.
Register a new taxonomy called "country".
Create a new category "Germany" with slug "germany".
Assign the category "Germany" to the post "Saksen".
Here is the code to register the taxonomy:
$labels = array(
'name' => _x( 'Countries', 'taxonomy general name', 'textdomain' ),
'singular_name' => _x( 'Country', 'taxonomy singular name', 'textdomain' ),
'search_items' => __( 'Search Countries', 'textdomain' ),
'popular_items' => __( 'Popular Countries', 'textdomain' ),
'all_items' => __( 'All Countries', 'textdomain' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Country', 'textdomain' ),
'update_item' => __( 'Update Country', 'textdomain' ),
'add_new_item' => __( 'Add New Country', 'textdomain' ),
'new_item_name' => __( 'New Country Name', 'textdomain' ),
'separate_items_with_commas' => __( 'Separate Countries with commas', 'textdomain' ),
'add_or_remove_items' => __( 'Add or remove Countries', 'textdomain' ),
'choose_from_most_used' => __( 'Choose from the most used Countries', 'textdomain' ),
'not_found' => __( 'No Countries found.', 'textdomain' ),
'menu_name' => __( 'Countries', 'textdomain' ),
);
$args = array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
//'rewrite' => array( 'slug' => 'country' ),
);
register_taxonomy( 'country', 'trafficoffice', $args );
Make sure the permalink settings of WordPress (Settings -> Permalinks) include the category.
/%category%/%postname%/
Read more about register_taxonomy().

Related

how to achieve this permalink abc.com/CPTName/CustomPostTypeCategorySlug/categoryname

I have created a cpt named blog. Registered taxonomy for blog as blog_category. I have a taxonomy-blog_category.php for displaying all categories of blog. When i click on a category the url i get is:
http://localhost/mysite/blog_category/category-name/
I need the url like this:
http://localhost/mysite/blog/blog_category/category-name/
function post_type_blog() {
$labels = array(
'name' => _x( 'Blog', 'post type general name' ),
'singular_name' => _x( 'Blog', 'post type singular name' ),
'add_new' => _x( 'Add New', 'Blog' ),
'add_new_item' => __( 'Add New Blog' ),
'edit_item' => __( 'Edit Blog' ),
'new_item' => __( 'New Blog' ),
'all_items' => __( 'All Blogs' ),
'view_item' => __( 'View Blog' ),
'search_items' => __( 'Search Blog' ),
'not_found' => __( 'No Blogs found' ),
'not_found_in_trash' => __( 'No Blogs found in the Trash' ),
'parent_item_colon' => '',
'menu_name' => 'Blog'
);
$args = array(
'labels' => $labels,
'description' => 'Add your Blog',
'public' => true,
'menu_position' => 3,
'menu_icon' => 'dashicons-admin-site',
'supports' => array( 'title', 'thumbnail','editor', 'page-attributes','excerpt','comments'),
'has_archive' => true,
);
register_post_type( 'blog', $args );
}
add_action( 'init', 'post_type_blog' );
function register_taxonomy_for_blog()
{
$labels = [
'name' => _x('Blog categories', 'taxonomy general name'),
'singular_name' => _x('Blog category', 'taxonomy singular name'),
'search_items' => __('Search Blog category'),
'all_items' => __('All Blog categories'),
'parent_item' => __('Parent Blog category'),
'parent_item_colon' => __('Parent Blog category:'),
'edit_item' => __('Edit Blog category'),
'update_item' => __('Update Blog category'),
'add_new_item' => __('Add New Blog category'),
'new_item_name' => __('New Blog category Name'),
'menu_name' => __('Blog categories'),
];
$args = [
'hierarchical' => true, // make it hierarchical (like categories)
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => ['slug' => 'blog_category'],
];
register_taxonomy('blog_category', ['blog'], $args);
}
add_action('init', 'register_taxonomy_for_blog');

"Nesting" of custom post types in Wordpress

I am working on a page and I just ran into a problem that I can't solve so I was hoping that somebody could help me out.
Let me illustrate my problem with a random example because I'm having a hard time explaining it.
So I created a custom 'movies' post type. I created a bunch of movies in my WP admin and I can access them by visiting domain.com/movies/moviename.
Now what if I want to separate the movies by their Genre. So I have Comedy, Action, Thriller. I want to have a page that showcases all the movies of one genre and each movie I want to be accessible through domain.com/movies/genre/moviename.
How do I get this structure? Is it somehow possible to create a sub-post type for each Genre?
Any help is appreciated!
What you're probably looking for is adding taxonomies to your custom post type:
https://codex.wordpress.org/Function_Reference/register_taxonomy
Here's an example usage. Assuming your Genres will be hierarchical, meaning, they can be nested. These are equivalent to Categories in Posts.(e.g. Thriller > Action, Thriller > Horror)
The second part is for a non-hierarchical taxonomy, meaning, it cannot be nested. These are equivalent to Tags in Posts. (e.g. #Steven Spielberg)
// hook into the init action and call create_movie_taxonomies when it fires
add_action( 'init', 'create_movie_taxonomies', 0 );
// create two taxonomies, genres and directors for the post type "movie"
function create_movie_taxonomies() {
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => _x( 'Genres', 'taxonomy general name', 'textdomain' ),
'singular_name' => _x( 'Genre', 'taxonomy singular name', 'textdomain' ),
'search_items' => __( 'Search Genres', 'textdomain' ),
'all_items' => __( 'All Genres', 'textdomain' ),
'parent_item' => __( 'Parent Genre', 'textdomain' ),
'parent_item_colon' => __( 'Parent Genre:', 'textdomain' ),
'edit_item' => __( 'Edit Genre', 'textdomain' ),
'update_item' => __( 'Update Genre', 'textdomain' ),
'add_new_item' => __( 'Add New Genre', 'textdomain' ),
'new_item_name' => __( 'New Genre Name', 'textdomain' ),
'menu_name' => __( 'Genre', 'textdomain' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'genre' ),
);
register_taxonomy( 'genre', array( 'movie' ), $args );
// Add new taxonomy, NOT hierarchical (like tags)
$labels = array(
'name' => _x( 'Directors', 'taxonomy general name', 'textdomain' ),
'singular_name' => _x( 'Director', 'taxonomy singular name', 'textdomain' ),
'search_items' => __( 'Search Directors', 'textdomain' ),
'popular_items' => __( 'Popular Directors', 'textdomain' ),
'all_items' => __( 'All Directors', 'textdomain' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Director', 'textdomain' ),
'update_item' => __( 'Update Director', 'textdomain' ),
'add_new_item' => __( 'Add New Director', 'textdomain' ),
'new_item_name' => __( 'New Director Name', 'textdomain' ),
'separate_items_with_commas' => __( 'Separate directors with commas', 'textdomain' ),
'add_or_remove_items' => __( 'Add or remove directors', 'textdomain' ),
'choose_from_most_used' => __( 'Choose from the most used directors', 'textdomain' ),
'not_found' => __( 'No directors found.', 'textdomain' ),
'menu_name' => __( 'Directors', 'textdomain' ),
);
$args = array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'director' ),
);
register_taxonomy( 'director', 'movie', $args );
}

Custom category metaboxes not functioning

I added some custom post types and custom taxonomies in my function.php and they work great. I assigned some hierarchical and non-hierarchical taxonomies (catagories & tags right?) to the custom post types. The non-hierarchical taxonomies work great and I can add new items in the tags metaboxes when editing the post. However, this does not work for my hierarchical taxonomies! I have no idea where the error lies, but the buttons "add new vehicle category" and "most used" do not work add all, you can click them but nothing loads. Is this something with javascript?
here's an screenshot to show the metaboxes I'm talking about:
Here's one of my registered custom post types:
$labels = array(
'name' => _x( 'Finished Projects', 'post type general name' ),
'singular_name' => _x( 'Finished Project', 'post type singular name' ),
'menu_name' => _x( 'Finished Projects', 'admin menu' ),
'name_admin_bar' => _x( 'Finished Project', 'add new on admin bar' ),
'add_new' => _x( 'Add New', 'Finished Project' ),
'add_new_item' => __( 'Add New Finished Project' ),
'new_item' => __( 'New Finished Project' ),
'edit_item' => __( 'Edit Finished Project' ),
'view_item' => __( 'View Finished Project' ),
'all_items' => __( 'All Finished Projects' ),
'search_items' => __( 'Search Finished Projects' ),
'parent_item_colon' => __( 'Parent Finished Projects:' ),
'not_found' => __( 'No Finished Projects found.' ),
'not_found_in_trash' => __( 'No Finished Projects found in Trash.' )
);
$args = array(
'labels' => $labels,
'description' => 'Page template for Finished Projects',
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'finished_projects' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array('excerpt','comments','author'),
'taxonomies' => array('scales', 'manufacturers','product categories','vehicle categories','countries'),
);
register_post_type( 'finishedprojects', $args );
and here's one of my registered custom taxonomies:
//Vehicle Categories
$labels = array(
'name' => _x( 'Vehicles Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Vehicle Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Vehicles Categories' ),
'all_items' => __( 'All Vehicles Categories' ),
'parent_item' => __( 'Parent Vehicle Category' ),
'parent_item_colon' => __( 'Parent Vehicle Category:' ),
'edit_item' => __( 'Edit Vehicle Category' ),
'update_item' => __( 'Update Vehicle Category' ),
'add_new_item' => __( 'Add New Vehicle Category' ),
'new_item_name' => __( 'New Vehicle Category Name' ),
'menu_name' => __( 'Vehicle Categories' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'vehicle_categories' ),
);
register_taxonomy( 'vehicle category', array( 'reviews','finishedprojects' ), $args );
Do you guys need more info or is this enough information?
Thanks!
Remove the space from the name of the taxonomy.
To do this, replace:
register_taxonomy( 'vehicle category', array( 'reviews','finishedprojects' ), $args );
with:
register_taxonomy( 'vehicle_category', array( 'reviews','finishedprojects' ), $args );
Also, replace:
'taxonomies' => array('scales', 'manufacturers','product categories','vehicle categories','countries'),
with:
'taxonomies' => array('scales', 'manufacturers','product_categories','vehicle_categories','countries'),

Why is wordpress making a query for attachment instead of taxonomy?

I made a custom post type and I've made a taxonomy registered to the cpt and the standard 'post'. This all works perfect.
Now I needed a 2nd taxonomy for organization reasons. I've simply copied the first one and changed the name and labels.
On the backend it all works normal, but on the front end clicking on it redirects to index.php. If I look via the debug toolbar at the query it is attachment=(term-name) instead of (taxonomy)=(term-name). Does anybody have any idea why wordpress does this? (Or to put it better, how did I make wordpress do this?)
This is how the taxonomies are registered, 'arweb' is the cpt and 'onderwerp' is the normal working taxonomy:
add_action( 'init', 'register_custom_taxonomies' );
function register_custom_taxonomies(){
// ///////////////////// ONDERWERP //////////////////////
$labels = array(
'name' => _x( 'Onderwerpen', 'taxonomy general name' ),
'singular_name' => _x( 'Onderwerp', 'taxonomy singular name' ),
'search_items' => __( 'Zoek Onderwerpen' ),
'popular_items' => __( 'Popular Onderwerpen' ),
'all_items' => __( 'All Onderwerpen' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Onderwerp' ),
'update_item' => __( 'Update Onderwerp' ),
'add_new_item' => __( 'Nieuw Onderwerp' ),
'new_item_name' => __( 'Naam Nieuw Onderwerp' ),
'add_or_remove_items' => __( 'Add or remove onderwerpen' ),
'choose_from_most_used' => __( 'Choose from the most used onderwerps' ),
'not_found' => __( 'No onderwerps found.' ),
'menu_name' => __( 'Onderwerpen' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'onderwerp' ),
);
register_taxonomy( 'onderwerp', array( "arweb","post" ), $args );
register_taxonomy_for_object_type( 'onderwerp', 'arweb' );
///////////////////// English onderwerp /////////////////////////////////////////////
$labels = array(
'name' => _x( 'Subjects', 'taxonomy general name' ),
'singular_name' => _x( 'Subject', 'taxonomy singular name' ),
'search_items' => __( 'Zoek Subjects' ),
'popular_items' => __( 'Popular Subjects' ),
'all_items' => __( 'All Subjecten' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Subject' ),
'update_item' => __( 'Update Subject' ),
'add_new_item' => __( 'Nieuw Subject' ),
'new_item_name' => __( 'Naam Nieuw Subject' ),
'add_or_remove_items' => __( 'Add or remove onderwerpen' ),
'choose_from_most_used' => __( 'Choose from the most used onderwerps' ),
'not_found' => __( 'No onderwerps found.' ),
'menu_name' => __( 'Subjecten' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
//'update_count_callback' => '_update_post_term_count',
'query_var' => true,
//'rewrite' => array( 'slug' => 'english-subject' ),
);
register_taxonomy( 'onderwerp_en', array( "arweb","post" ), $args );
register_taxonomy_for_object_type( 'onderwerp_en', 'post' );
register_taxonomy_for_object_type( 'onderwerp_en', 'arweb' );
}
Try updating your permalinks (by going to Settings .. Permalinks and saving). It's possible the URL is matching an outdated rewrite rule.

Wordpress custom taxonomy move meta_box

I have a custom meta box(taxonomy) that is positioned on side of my custom post. I want to move it to normal position, but when I remove it(remove_meta_box), and re-add(add_meta_box) I only get bar, with no options to select anything. I think I didn't write proper $callback, but I tried many variations, and don't have a clue.
function create_isotope_taxonomies()
{
$labels = array(
'name' => _x( 'Select Category', 'taxonomy general name' ),
'singular_name' => _x( 'Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Categories' ),
'popular_items' => __( 'Popular Categories' ),
'all_items' => null,
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit' ),
'update_item' => __( 'Update' ),
'add_new_item' => __( 'Add New' ),
'new_item_name' => __( 'New Category' ),
'separate_items_with_commas' => __( 'Separate writers with commas' ),
'add_or_remove_items' => __( 'Add or remove categories' ),
'choose_from_most_used' => __( 'Choose from the most used categories' ),
'menu_name' => __( 'Categories' ),
);
register_taxonomy('fzisotope_categories','fzisotope_post',array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'fzisotope_categories' ),
));
}
function fzisotope_categories_meta_box(){
remove_meta_box('fzisotope_categoriesdiv', 'fzisotope_post', 'side');
add_meta_box( 'fzisotope_categoriesdiv', 'Select Category', 'fzisotope_categories_meta_box', 'fzisotope_post', 'normal', 'high');
//print '<pre>';print_r( $wp_meta_boxes['post'] );print '<pre>';
}
add_action( 'admin_init', 'fzisotope_categories_meta_box', 0 );
Thanks for reading
You have to use:
post_categories_meta_box for taxonomies with hierarchies.
remove_meta_box('fzisotope_categoriesdiv', 'fzisotope_post', 'side');
add_meta_box( 'fzisotope_categoriesdiv', 'XXXXXXXXXXX', 'post_categories_meta_box', 'post', 'normal', 'high', array( 'taxonomy' => 'fzisotope_categories' ));

Resources