Skip to content

Instantly share code, notes, and snippets.

@amilabandara
Created January 21, 2017 12:24
Show Gist options
  • Save amilabandara/85297c52a491e15ec2ba69468195871f to your computer and use it in GitHub Desktop.
Save amilabandara/85297c52a491e15ec2ba69468195871f to your computer and use it in GitHub Desktop.
<?php
//hierarchical
//hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'create_topics_hierarchical_taxonomy', 0 );
//create a custom taxonomy name it topics for your posts
function create_topics_hierarchical_taxonomy() {
// Add new taxonomy, make it hierarchical like categories
//first do the translations part for GUI
$labels = array(
'name' => _x( 'Topics', 'taxonomy general name' ),
'singular_name' => _x( 'Topic', 'taxonomy singular name' ),
'search_items' => __( 'Search Topics' ),
'all_items' => __( 'All Topics' ),
'parent_item' => __( 'Parent Topic' ),
'parent_item_colon' => __( 'Parent Topic:' ),
'edit_item' => __( 'Edit Topic' ),
'update_item' => __( 'Update Topic' ),
'add_new_item' => __( 'Add New Topic' ),
'new_item_name' => __( 'New Topic Name' ),
'menu_name' => __( 'Topics' ),
);
// Now register the taxonomy
register_taxonomy('topics',array('post'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'topic' ),
));
}
//non-hierarchical
//hook into the init action and call create_topics_nonhierarchical_taxonomy when it fires
add_action( 'init', 'create_topics_nonhierarchical_taxonomy', 0 );
function create_topics_nonhierarchical_taxonomy() {
// Labels part for the GUI
$labels = array(
'name' => _x( 'Topics', 'taxonomy general name' ),
'singular_name' => _x( 'Topic', 'taxonomy singular name' ),
'search_items' => __( 'Search Topics' ),
'popular_items' => __( 'Popular Topics' ),
'all_items' => __( 'All Topics' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Topic' ),
'update_item' => __( 'Update Topic' ),
'add_new_item' => __( 'Add New Topic' ),
'new_item_name' => __( 'New Topic Name' ),
'separate_items_with_commas' => __( 'Separate topics with commas' ),
'add_or_remove_items' => __( 'Add or remove topics' ),
'choose_from_most_used' => __( 'Choose from the most used topics' ),
'menu_name' => __( 'Topics' ),
);
// Now register the non-hierarchical taxonomy like tag
register_taxonomy('topics','post',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' => 'topic' ),
));
}
/************************************************
stack exchange example
************************************************/
/*First of all if you want to show taxonomy metabox only to custom post type then
register the taxonomy to only that custom post type by passing the custom post type
name as argument in register_taxonomy. By doing this the taxonomy metabox appears only
to custom post type. If your don't want to show category metabox to custom post type
then remove the term category as argument while registering custom post type and instead
include the taxonomy slug name like this 'taxonomies' => array( 'post_tag','your_taxonomy_name').
here is the code how i achieved that. I have registered a custom taxonomy with slug themes_categories
under under custom post type themes**/
function themes_taxonomy() {
register_taxonomy(
'themes_categories', //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
'themes', //post type name
array(
'hierarchical' => true,
'label' => 'Themes store', //Display name
'query_var' => true,
'rewrite' => array(
'slug' => 'themes', // This controls the base slug that will display before each term
'with_front' => false // Don't display the category base before
)
)
);
}
add_action( 'init', 'themes_taxonomy');
/*
Then to change the permalink i have created following function
*/
function filter_post_type_link($link, $post)
{
if ($post->post_type != 'themes')
return $link;
if ($cats = get_the_terms($post->ID, 'themes_categories'))
$link = str_replace('%themes_categories%', array_pop($cats)->slug, $link);
return $link;
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);
//
First of all if you want to show taxonomy metabox only to custom post type then register the taxonomy to only that custom post type by passing the custom post type name as argument in register_taxonomy. By doing this the taxonomy metabox appears only to custom post type. If your don't want to show category metabox to custom post type then remove the term category as argument while registering custom post type and instead include the taxonomy slug name like this 'taxonomies' => array( 'post_tag','your_taxonomy_name'). here is the code how i achieved that. I have registered a custom taxonomy with slug themes_categories under under custom post type themes
function themes_taxonomy() {
register_taxonomy(
'themes_categories', //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
'themes', //post type name
array(
'hierarchical' => true,
'label' => 'Themes store', //Display name
'query_var' => true,
'rewrite' => array(
'slug' => 'themes', // This controls the base slug that will display before each term
'with_front' => false // Don't display the category base before
)
)
);
}
add_action( 'init', 'themes_taxonomy');
Then to change the permalink i have created following function
function filter_post_type_link($link, $post)
{
if ($post->post_type != 'themes')
return $link;
if ($cats = get_the_terms($post->ID, 'themes_categories'))
$link = str_replace('%themes_categories%', array_pop($cats)->slug, $link);
return $link;
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);
//Then i registered a custom post type with slug themes as below
//Registering Custom Post Type Themes
add_action( 'init', 'register_themepost', 20 );
function register_themepost() {
$labels = array(
'name' => _x( 'Themes', 'my_custom_post','custom' ),
'singular_name' => _x( 'Theme', 'my_custom_post', 'custom' ),
'add_new' => _x( 'Add New', 'my_custom_post', 'custom' ),
'add_new_item' => _x( 'Add New ThemePost', 'my_custom_post', 'custom' ),
'edit_item' => _x( 'Edit ThemePost', 'my_custom_post', 'custom' ),
'new_item' => _x( 'New ThemePost', 'my_custom_post', 'custom' ),
'view_item' => _x( 'View ThemePost', 'my_custom_post', 'custom' ),
'search_items' => _x( 'Search ThemePosts', 'my_custom_post', 'custom' ),
'not_found' => _x( 'No ThemePosts found', 'my_custom_post', 'custom' ),
'not_found_in_trash' => _x( 'No ThemePosts found in Trash', 'my_custom_post', 'custom' ),
'parent_item_colon' => _x( 'Parent ThemePost:', 'my_custom_post', 'custom' ),
'menu_name' => _x( 'Themes Posts', 'my_custom_post', 'custom' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'description' => 'Custom Theme Posts',
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'post-formats', 'custom-fields' ),
'taxonomies' => array( 'post_tag','themes_categories'),
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => get_stylesheet_directory_uri() . '/functions/panel/images/catchinternet-small.png',
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'query_var' => true,
'can_export' => true,
'rewrite' => array('slug' => 'themes/%themes_categories%','with_front' => FALSE),
'public' => true,
'has_archive' => 'themes',
'capability_type' => 'post'
);
register_post_type( 'themes', $args );//max 20 charachter cannot contain capital letters and spaces
}
/*
There are few things you have to remember while registering custom post. change the has_archive parameter to
custom post type slug name and another is change the rewrite slug name as 'slug' => 'custom_post_type_slug/%taxonomy_slug%
Now when you add a new post type in the write post type page... you will see the permalink as
http://www.example.com/wordpress/themes/%themes_categories%/post-name/. If the custom taxonomy
for this post is not selected then the permalink will remain http://www.example.com/wordpress/themes/%themes_categories%/post-name/
which will then show a bad request. To correct this we create a default term in the custom taxonomy. (same as uncategorized in
categories) Add this to functions.php
*/
function default_taxonomy_term( $post_id, $post ) {
if ( 'publish' === $post->post_status ) {
$defaults = array(
'themes_categories' => array( 'other'), //
);
$taxonomies = get_object_taxonomies( $post->post_type );
foreach ( (array) $taxonomies as $taxonomy ) {
$terms = wp_get_post_terms( $post_id, $taxonomy );
if ( empty( $terms ) && array_key_exists( $taxonomy, $defaults ) ) {
wp_set_object_terms( $post_id, $defaults[$taxonomy], $taxonomy );
}
}
}
}
add_action( 'save_post', 'default_taxonomy_term', 100, 2 );
/*
Now when the custom taxonomy is left blank the permlaink becomes http://www.example.com/wordpress/themes/other/post-name/ automatically.
At last don't forget to flush the rewrite by clicking on save changes in permalink setting in admin section
otherwise you will be redirected to 404 error. Hope this helps you.
http://wordpress.stackexchange.com/questions/57493/custom-taxonomy-specific-to-a-custom-post-type
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment