0

I have a custom post type with a custom taxonomy.

When the custom taxonomy is set to "bar", I want to disable comments with:

wp_update_post(array(
  'ID' => $post->ID,
  'comment_status' => "closed"
));

I think this function triggers the save/update hooks, so I can't trigger this function on those hooks (e.g. save_post_custom_post_type) or it'll be an infinite loop.

I've looked for a hook like updated_post_meta that applies on taxonomy/terms only but no luck.

1 Answer 1

1

You can avoid infinite loop by removing the action before calling update post function.

function maybe_disable_comment($post_id) {
    // do you check if the post has specific custom tax term.
    if ( ! has_term( 'bar', 'YOUR_CUSTOM_TAX', $post_id ) ) {
        return false;
    }

    // remove the filter that will create infinite loop.
    remove_action( 'wp_insert_post', 'maybe_disable_comment' );

    // disable comment.
    wp_update_post( array(
       'ID'             => $post_id,
       'comment_status' => "closed"
    ) );

    // add the action again. (so that next post update hook is handled properly)
    add_action( 'wp_insert_post', 'maybe_disable_comment' );
}
add_action('wp_insert_post', 'maybe_disable_comment');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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