• Resolved Jon

    (@freshyjon)


    I have a custom post type with 3 different taxonomies. Is there a way to specify the “default” term for each of the taxonomies?

    I would like it so, when a user begins to create a new post, a “default” term (one that I specify) is already selected for a taxonomy.

    This way, the post would automatically receive the default term, without the user having to select it. But if they want, they could of course change their selection to something else.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Not something we have built into the plugin at all, and I’m not aware of any ready-made plugins that handle this. Some searching around lead me to this snippet via https://circlecube.com/says/2013/01/set-default-terms-for-your-custom-taxonomy-default/:

    <?php
    /**
    * Define default terms for custom taxonomies in WordPress 3.0.1
    *
    * @author Michael Fields https://wordpress.mfields.org/
    * @props John P. Bloch https://www.johnpbloch.com/
    * @props Evan Mulins https://circlecube.com/circlecube/
    *
    * @since 2010-09-13
    * @alter 2013-01-31
    *
    * @license GPLv2
    */
    function mfields_set_default_object_terms( $post_id, $post ) {
    	if ( 'publish' === $post->post_status && $post->post_type === 'your_custom_post_type' ) {
    		$defaults = array(
    			'your_taxonomy_id' => array( 'your_term_slug' )
    		);
    		$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 );
    

    You would need to fill in the appropriate spots for the post type and taxonomy values. Since you’re dealing with 3, you may need to amend briefly to make it work for all at once. However, as a whole it should be possible. You’d want to save it to your theme’s functions.php or similar.

    Thread Starter Jon

    (@freshyjon)

    Yeah, I’ve found this solution as well… but I’m trying to find something that already has it “pre-selected” when the user starts creating the post. Example, if it’s a list of terms, the “default” term(s) would already be checked/enabled.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Noted and understood. Wish I had more for you at the moment, with that said.

    Just wanted to add to Jon’s post, this is something that i’m after as well.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Set a default taxonomy term for Custom Post Type’ is closed to new replies.