I have read through every blog post I could find, but nothing out there seems to work for me. All I would need to do is to have WP automatically assign a default taxonomy/category (“newest”) to my custom post type “photos”, so that when a user adds a new photo, the “newest” category is already selected and assigned (like for the “uncategorised” for a normal blog post).
declare ( encoding = 'UTF-8' );
! defined( 'ABSPATH' ) and exit;
add_action( 'init', array ( 'MCP_Photos', 'init' ) );
class MCP_Photos
{
/**
* Creates a new instance.
*
* @wp-hook init
* @see __construct()
* @return void
*/
public static function init()
{
new self;
}
/**
* Constructor
*/
public function __construct()
{
$labels = array(
'name' => 'Photography',
'singular_name' => 'Photo',
'add_new' => 'Add New',
'add_new_item' => 'Add New Photo',
'edit_item' => 'Edit Photo',
'new_item' => 'New Photo',
'all_items' => 'All Photos',
'view_item' => 'View Photo',
'search_items' => 'Search Photos',
'not_found' => 'No Photos found',
'not_found_in_trash' => 'No Photos found in Trash',
'parent_item_colon' => '',
'menu_name' => 'Photography'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array(
'with_front' => false,
'slug' => "photo"
),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => true,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
'taxonomies' => array('post_tag')
);
register_post_type("photos", $args);
// Prevent WordPress from sending a 404 for our new perma structure.
add_rewrite_rule(
'^photo/(d+)/[^/]+/?$',
'index.php?post_type=photos&p=$matches[1]',
'top'
);
// Inject our custom structure.
add_filter( 'post_type_link', array ( $this, 'fix_permalink' ), 1, 2 );
}
/**
* Filter permalink construction.
*
* @wp-hook post_type_link
* @param string $post_link default link.
* @param int $id Post ID
* @return string
*/
public function fix_permalink( $post_link, $id = 0 )
{
$post = &get_post($id);
if ( is_wp_error($post) || $post->post_type != 'photos' )
{
return $post_link;
}
// preview
empty ( $post->slug )
and $post->slug = sanitize_title_with_dashes( $post->post_title );
return home_url(
user_trailingslashit( "photo/$post->ID/$post->slug" )
);
}
}
// ----------------------------- add photography categories taxonomy ----------------------------------
function create_photo_categories() {
register_taxonomy(
'photography', // name of the taxonomy
'photos', // for which post type it applies
array(
'labels' => array(
'name' => 'Categories',
'add_new_item' => 'Add New Category',
'new_item_name' => "New Category"
),
'show_ui' => true,
'show_tagcloud' => false,
'hierarchical' => true
)
);
}
add_action( 'init', 'create_photo_categories', 0 );
I just found this old question with no correct and complete answer. So I’m writing this to anyone interested in this topic.
I will explain in detail:
How to Register CPT & Custom Taxonomy & Add a Default Term (non-removable)
And how to register this default term to our custom post types, when no other term is selected via CPT metabox.
Steps to Proceed:
register_post_type
function after init.register_taxonomy
function after registering CPT.Note: I’ve changed the structure of the code presented in the question and used singleton approach and removed parts specific to this question (e.g. rewrite rule etc.) I also changed the CPT name to
photo
, taxonomy name togallery_cat
and default term slug todefault_gallery_cat
.Note: You can assign only one term as default taxonomy term.
I’ve written the other details as comments, in the code. It is a functional WP plugin as it is.
Final code:
Completely tested and it’s working on WP 5.1
Note: save_post_{$post->post_type} hook is used to add default category term when saving a
photo
CPT.P.S. I may complete and post this code on Github in the near future.
Look for something like this in your
register_post_type
function’s$args
— >Remove It
To register taxonomies for your custom post type use function
Register Taxonomy
Add taxonomies like this..
Total code become like this for admin:
Edit
wp-includes/taxonomy.php
and write your custom post type name where it says'custom post name here'
: