How to set default taxonomy (category) for custom post types in WP (ok)
https://stackoverflow.com/questions/19706742/how-to-set-default-taxonomy-category-for-custom-post-types-in-wp
Ví dụ 1:
<?php
class Photo {
/**
* Instance of Photo
* @access protected
* @var object $instance The instance of Photo.
*/
private static $instance = null;
/**
* Photo constructor.
* This is a private constructor to be used in getInstance method to do things in a singleton way
* It's a good idea to leave this constructor empty and make `init` method public to use it outside of the class, which is a good thing for Unit Testing
* @access private
*/
private function __construct() {
$this->init();
}
/**
* Initialize the plugin.
* You can make it public and use it outside of the class
* @access private
* @return void
*/
private function init() {
// It's possible to use one method to cover these and hook it to `init`. I just like the way using single purpose OOP methods.
// Note the priorities
add_action('init', [$this, 'register_cpt'], 10);
add_action('init', [$this, 'register_gallery_cat_tax'], 11);
add_action('init', [$this, 'insert_default_gallery_cat_term'], 12);
// `save_post_{$post->post_type}` hook is used. Doc: https://developer.wordpress.org/reference/hooks/save_post_post-post_type/
add_action('save_post_photo', [$this, 'set_default_gallery_cat'], 99, 2);
}
/**
* Register `Photo` CPT and `gallery_cat` taxonomy to it
* This should be done after `init`
* @access public
* @wp-hook init
* @return void
*/
public function register_cpt() {
$labels = array(
'name' => 'Photos',
'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(
'public' => true,
'show_in_menu' => true,
'labels' => $labels,
'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments'),
'taxonomies' => array('post_tag', 'gallery_cat'),
);
register_post_type('photo', $args);
}
/**
* Register `gallery_cat` taxonomy
* This should be done after registering CPT
* @access public
* @wp-hook init
* @return void
*/
public function register_gallery_cat_tax() {
$labels = [
'name' => 'Gallery Categories',
'singular_name' => 'Gallery Category',
'all_items' => 'All Gallery Categories',
'edit_item' => 'Edit Category',
'view_item' => 'View Category',
'update_item' => 'Update Category',
'add_new_item' => 'Add New Category',
'new_item_name' => 'Category Name',
'parent_item' => 'Parent Category',
'parent_item_colon' => 'Parent Category:',
'search_items' => 'Search Gallery Categories',
'popular_items' => 'Popular Categories',
];
register_taxonomy(
'gallery_cat',
'photo',
array(
'labels' => $labels,
'show_ui' => true,
'show_tagcloud' => false,
'hierarchical' => true,
)
);
}
/**
* Insert default gallery_cat
* `default_{$taxonomy}` option is used to make this term as default `gallery_cat` term (non-removable)
* @access public
* @wp-hook init
*/
public function insert_default_gallery_cat_term() {
// check if category(term) exists
$cat_exists = term_exists('default_gallery_cat', 'gallery_cat');
if (!$cat_exists) {
// if term is not exist, insert it
$new_cat = wp_insert_term(
'Default Gallery Name',
'gallery_cat',
array(
'description' => 'This is your default gallery category',
'slug' => 'default_gallery_cat',
)
);
// wp_insert_term returns an array on success so we need to get the term_id from it
$default_cat_id = ($new_cat && is_array($new_cat)) ? $new_cat['term_id'] : false;
} else {
//if default category is already inserted, term_exists will return it's term_id
$default_cat_id = $cat_exists;
}
// Setting default_{$taxonomy} option value as our default term_id to make them default and non-removable (like default uncategorized WP category)
$stored_default_cat = get_option('default_gallery_cat');
if (empty($stored_default_cat) && $default_cat_id) {
update_option('default_gallery_cat', $default_cat_id);
}
}
/**
* Add an default `gallery_cat` taxonomy term for `photo` CPT on save
* If no `gallery_cat` is selected, default gallery_cat will be registered to the post
* @access public
* @wp-hook save_post_photo
* @param integer $post_id
* @param object $post
*/
public function set_default_gallery_cat($post_id, $post) {
if ('publish' === $post->post_status) {
$gallery_cats = wp_get_post_terms($post_id, 'gallery_cat');
$default_gallery_cat = (int) get_option('default_gallery_cat');
if (empty($gallery_cats)) {
wp_set_object_terms($post_id, $default_gallery_cat, 'gallery_cat');
}
}
}
/**
* Instance
* Used to retrieve the instance of this class.
* @access public
* @return object $instance of the class
*/
static public function getInstance() {
if (self::$instance == NULL) {
self::$instance = new self();
}
return self::$instance;
}
}
// Run this
Photo::getInstance();
?>
Ví dụ 2:
class Test {
private static $instance = null;
private function __construct() {
$this->init();
}
private function init() {
add_action('init', [$this, 'register_cpt'], 10);
add_action('init', [$this, 'register_gallery_cat_tax'], 11);
add_action('init', [$this, 'insert_default_gallery_cat_term'], 12);
add_action('save_post_book', [$this, 'set_default_gallery_cat'], 99, 2);
}
public function register_cpt() {
$args = array(
'public' => true,
'label' => __( 'Books', 'textdomain' ),
'menu_icon' => 'dashicons-book',
);
register_post_type( 'book', $args );
}
public function register_gallery_cat_tax() {
$args = array(
'label' => __( 'Genre', 'textdomain' ),
'hierarchical' => true,
);
register_taxonomy( 'genre', 'book', $args);
}
public function insert_default_gallery_cat_term() {
$cat_exists = term_exists('default_genre', 'genre');
if (!$cat_exists) {
$new_cat = wp_insert_term(
'Default Book',
'genre',
array(
'description' => 'This is your default Book category',
'slug' => 'default_genre',
)
);
$default_cat_id = ($new_cat && is_array($new_cat)) ? $new_cat['term_id'] : false;
}else {
$default_cat_id = $cat_exists;
}
$stored_default_cat = get_option('default_genre');
if (empty($stored_default_cat) && $default_cat_id) {
update_option('default_genre', $default_cat_id);
}
}
public function set_default_gallery_cat($post_id, $post) {
if ('publish' === $post->post_status) {
$book_cats = wp_get_post_terms($post_id, 'genre');
$default_book_cat = (int) get_option('default_genre');
if (empty($book_cats)) {
wp_set_object_terms($post_id, $default_book_cat, 'genre');
}
}
}
public static function getInstance() {
if(self::$instance == null) {
self::$instance = new self();
}
return self::$instance;
}
}
Test::getInstance();
PreviousTạo thêm một cột trong table post và tìm kiếm nội dung của chúng (ok) có một cách khác dùng js :(NextShowing custom taxonomy column in custom posts type listings (ok)
Last updated
Was this helpful?