I try to assign custom taxonomies to a page when newly added by the "publish" button.

This is the function:

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

    log_me ('From inside function: '.__FUNCTION__.', while I pressed the "publish" button. Post-ID: '.$id);

    $taxonomy_ar = get_terms( 'property-features', '' );

    foreach ($taxonomy_ar as $taxonomy_term) {          
        log_me ('Inside the function: '.__FUNCTION__.' and inside the "foreach"-loop for the ID: '.$id.' and Term: '. $taxonomy_term->name . ' and Post-ID :'. $post->ID);
        wp_set_object_terms( $post->ID, $taxonomy_ar, $taxonomy_term->name, true );  

and here is the hook:

add_action( 'save_post', 'set_default_object_terms', 100, 2 );

In the logfile which i added to figure out if i find all my values, all my custom taxonomies get found:

[13-May-12 16:28] be in function while "publish" is pressed with this id: **64**
[13-May-12 16:28] In the "foreach" with id: 64 and Term: **Kitchen** and Post-ID :**64**
[13-May-12 16:28] In the "foreach" with id: 64 and Term: **Stove** and Post-ID :**64**
[13-May-12 16:28] In the "foreach" with id: 64 and Term: **Pets ok** and Post-ID :**64**

But it does not assign it. Does somebody know where the trick is?

  • What exactly is log_me()? – kaiser May 13 '12 at 13:09
  • that is a function that writes into the debug.log – JSS May 13 '12 at 14:46
  • That's what I suspected :) I wanted to see the function itself out of interest. Btw: Logging is also possible with define( 'WP_DEBUG_LOG', true );. – kaiser May 13 '12 at 15:46
  • 1
    here it comes:// With debugging enabled, we write errors in: "/wp-content/debug.log" . function log_me($message) { if (WP_DEBUG === true) { if (is_array($message) || is_object($message)) { error_log(print_r($message, true)); } else { error_log($message); } } } – JSS May 13 '12 at 16:26

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;

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

  • Thanks so much. That was very close. Just needed to change from $taxonomy_term->ID to $taxonomy_term->name and then it worked in the right way. I did the register_taxo... like this: function reg_tax() { register_taxonomy_for_object_type('property-features', 'page'); add_post_type_support('page', 'property-features'); } – JSS May 13 '12 at 14:48
  • wp_set_object_terms only accepts ID or term slug , not names. – Bainternet May 13 '12 at 16:02
  • Sorry to say, but it didnt do it with the ID. I used your snippet and just changed from: $term_ids[] = $taxonomy_term->ID; to $term_ids[] = $taxonomy_term->name; And it worked. Dont ask me why. – JSS May 13 '12 at 16:31

I've got a snippet that does this on load of the edit page, instead of on save. (So the user can see the selection before they hit publish, no surprise default terms being assigned on post_save).

I had a function changing the checkboxes to radio buttons already but it seemed like the appropriate place to also add checked to one of the inputs.

Works on Add new, works on edit of an existing item.

// Press type taxonomy
// - Replace checkboxes for radio buttons
// - Select "Press release" by default
add_filter('wp_terms_checklist_args', 'press_type_radio_checklist');

function press_type_radio_checklist($args)

    $radio_taxonomies = [

    if (!empty($args['taxonomy']) && in_array($args['taxonomy'], $radio_taxonomies)) {
        if (empty($args['walker']) || is_a($args['walker'], 'Walker')) {

            // Radio button taxonomy walker
            if (!class_exists('Walker_Radio_Checklist')) {
                class Walker_Radio_Checklist extends Walker_Category_Checklist

                    function walk($elements, $max_depth, $args = [])

                        $output = parent::walk($elements, $max_depth, $args);
                        $output = str_replace([
                        ], [
                        ], $output);

                        // If there aren't any selected cats, select the default
                        if (empty($args['selected_cats'])) {
                            $default_term = get_term_by('name', 'Press release', 'press_type');

                            if (!empty($default_term) && $default_term instanceof WP_Term) {
                                $default_term_id = $default_term->term_id;

                                $output = str_replace([
                                    'input value="' . $default_term_id . '"',
                                    "input value='$default_term_id'",
                                ], [
                                    'input value="' . $default_term_id . '" checked',
                                    "input value='$default_term_id' checked",
                                ], $output);

                        return $output;

            $args['walker'] = new Walker_Radio_Checklist;

    return $args;
New contributor
golden_grahams is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

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.