Is there some option hook to set a default category for a custom_post_type when the user didn't choose any ?

For example in RoR it's very simple - by using before_save filter you can set whatever you want before update model, or before_create - before creating it ...

How can I hook some action after the user clicks Publish but before the action ends .. and check if some category was choosen for a custom_post_type - if not, set some default?


Maybe this is what you are looking for?

Source is from @Michael Fields

 * Set default cat for cpt
 * @source {https://circlecube.com/says/2013/01/set-default-terms-for-your-custom-taxonomy-default/}
 * @source {http://wordpress.mfields.org/2010/set-default-terms-for-your-custom-taxonomies-in-wordpress-3-0/}
 * @license   GPLv2
function set_default_object_terms_203962( $post_id, $post ) {
if ( 'publish' === $post->post_status ) {
    $defaults = array(
        //'your_taxonomy_id' => array( 'your_term_slug', 'your_term_slug' )
        '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', 'set_default_object_terms_203962', 100, 2 );

This works like a charm for us.

ps, there is also a plugin which could be helpfull to 'force' some of your wishes before an user(read editor/author etc.) even can publish.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.