How to assign default taxonomy to pages on ‘save_post’?

You are using wp_set_object_terms wrong, the second parameter should be the term slug or id and the 3 parameter should be the taxonomy name, so try:

function set_default_object_terms( $id, $post ) {
    if ( 'publish' === $post->post_status ) {

        log_me ('be in function while "publish" i pressed with this id: '.$id);

        $taxonomy_ar = get_terms( 'property-features' );
        if (count($taxonomy_ar) > 0){
            foreach ($taxonomy_ar as $taxonomy_term) {          
                //create an arry with all term ids
                log_me ('In the "foreach" with id: '.$id.' and Term: '. $taxonomy_term->name . ' and Post-ID :'. $post->ID);
                $term_ids[] = $taxonomy_term->ID;
            }
            wp_set_object_terms($post->ID,$term_ids,'property-features',true);
        }
    }
}

and make sure you register the taxonomy for pages type using register_taxonomy_for_object_type ex:

register_taxonomy_for_object_type('property-features','page');

Leave a Comment