How to assign custom taxonomy values to CPT
-
I have a CPT and a custom taxonomy assigned to it named ‘media_archive_categories’. They work like regular post categories, but are specific to my CPT. Anyway, I am having a hard time wrapping my head around the proper way to create the custom taxonomy if it doesnt exist and return it id or if if it does, simply return its id so that i can assign it to a post. I know I shouldnt be using wp_create_category as thats only for native categories, but I figured this might be the best way for me to least show you what I am trying to do. Here is test code so far:
<?php if ( ! function_exists( 'media_archive_categories' ) ) { // Register Custom Taxonomy function media_archive_categories() { $labels = array( 'name' => _x( 'Categories', 'Taxonomy General Name', 'text_domain' ), 'singular_name' => _x( 'Category', 'Taxonomy Singular Name', 'text_domain' ), 'menu_name' => __( 'Categories', 'text_domain' ), 'all_items' => __( 'All Categories', 'text_domain' ), 'parent_item' => __( 'Parent Category', 'text_domain' ), 'parent_item_colon' => __( 'Parent Category:', 'text_domain' ), 'new_item_name' => __( 'New Category Name', 'text_domain' ), 'add_new_item' => __( 'Add New Category', 'text_domain' ), 'edit_item' => __( 'Edit Category', 'text_domain' ), 'update_item' => __( 'Update Category', 'text_domain' ), 'view_item' => __( 'View Category', 'text_domain' ), 'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ), 'add_or_remove_items' => __( 'Add or remove category', 'text_domain' ), 'choose_from_most_used' => __( 'Choose from the most used', 'text_domain' ), 'popular_items' => __( 'Popular Categories', 'text_domain' ), 'search_items' => __( 'Search Categories', 'text_domain' ), 'not_found' => __( 'Not Found', 'text_domain' ), 'no_terms' => __( 'No Categories', 'text_domain' ), 'items_list' => __( 'Category list', 'text_domain' ), 'items_list_navigation' => __( 'Category list navigation', 'text_domain' ), ); $args = array( 'labels' => $labels, 'hierarchical' => true, 'public' => true, 'show_ui' => true, 'show_admin_column' => true, 'show_in_nav_menus' => false, 'show_tagcloud' => false, ); register_taxonomy( 'media_archive_categories', array( 'media_archive' ), $args ); } add_action( 'init', 'media_archive_categories', 0 ); } function create_media_post($media_data) { // Category created & categary id is returned. // If the category already exists, it is not duplicated. The ID of the original existing category is returned without error. // You can use $id as category id $cat_id = wp_create_category($media_data['series']); // Create post object $my_post_data = array( 'post_type' => 'media_archive', 'post_title' => wp_strip_all_tags( $title ), 'post_date' => mysql2date('M j Y', $media_data["event_date"]), 'post_excerpt' => $media_data['series'], 'tax_input' => array(), 'post_status' => 'publish', 'post_author' => 1, ); // Insert the post into the database and get back post_id $post_id = wp_insert_post( $my_post_data ); add_post_meta($post_id, 'event_id', $media_data['event_id']); add_post_meta($post_id, 'speaker', $media_data['speaker']); add_post_meta($post_id, 'video_url', $media_data['video_url']); add_post_meta($post_id, 'audio_url', $media_data['audio_url']); add_post_meta($post_id, 'info_url', $media_data['info_url']); #$attach_id = wp_insert_attachment( $attachment, $filename, $post_id ); #$attach_data = wp_generate_attachment_metadata( $attach_id, $filename ); #wp_update_attachment_metadata( $attach_id, $attach_data ); } $media_data = array('event_id' => '11111', 'event_date' => 'Wed, 10 Jan 2007 03:00:00 +0000', 'excerpt' => 'test excerpt', 'speaker' => 'Test Speaker', 'video_url' => 'https://www.google.com/1.html', 'audio_url' => 'https://www.google.com/2.html', 'info_url' => 'https://www.google.com/3.html', 'series' => 'Test Series' ); create_media_post($media_data); ?>
- The topic ‘How to assign custom taxonomy values to CPT’ is closed to new replies.