Forcing a term/taxonomy on all posts of CPT
-
I have a CPT – ‘Events’ with 400+ posts [with no terms in the custom taxonomy (tax_category) selected] in them, as I recently imported those posts.
Now, I’ve created a URL structure where I need to have some term of tax_category assigned to each post. So, is there any way with which I can assign a term from tax_category (say ‘New Gigs’) to all posts of the Events cpt?
While searching I also found the following code, but am confused of how can I modify it suit my need or it even is the solution am looking for? –
function mfields_set_default_object_terms( $post_id, $post ) { if ( 'publish' === $post->post_status ) { $defaults = array( 'post_tag' => array( 'taco', 'banana' ), 'monkey-faces' => array( 'see-no-evil' ), ); $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', 'mfields_set_default_object_terms', 100, 2 );
Code Source – https://wordpress.mfields.org/2010/set-default-terms-for-your-custom-taxonomies-in-wordpress-3-0/
- The topic ‘Forcing a term/taxonomy on all posts of CPT’ is closed to new replies.