categories - When creating a new product, auto assign it to all custom taxonomy woocommerce

I have a custom taxonomy called Location, and there are a lot of parents and children categories on it to mark in which

I have a custom taxonomy called Location, and there are a lot of parents and children categories on it to mark in which location the product is available. They are available in the most of the categories so I end up expending too much time ticking them.

Is it possible to include a function to tick all the categories and I just need to untick the locations where it is not available?

I have a custom taxonomy called Location, and there are a lot of parents and children categories on it to mark in which location the product is available. They are available in the most of the categories so I end up expending too much time ticking them.

Is it possible to include a function to tick all the categories and I just need to untick the locations where it is not available?

Share Improve this question asked Feb 28, 2020 at 9:18 Leandro AndradeLeandro Andrade 171 gold badge1 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

For post types not using the block editor, you can use wp_terms_checklist_args filter to manipulate the selected terms of a hierarchical taxonomy. You can use the filter to set pre-selected terms like so,

function auto_check_hierarchial_terms_on_new_post( $args, $post_id ) {
  // is it new post?
  if ( 'auto-draft' !== get_post_status($post_id) ) {
    return $args;
  }
  // is it my taxonomy?
  if ( 'my_custom_taxonomy' !== $args['taxonomy'] ) {
    return $args;
  }
  // let's not overwrite anything by accident
  if ( ! empty( $args['selected_cats'] ) ) {
    return $args;
  }
  // get existing terms
  $terms = get_terms( array(
    'taxonomy' => $args['taxonomy'],
    'hide_empty' => false,
  ) );
  if ( ! $terms || ! is_array( $terms ) ) {
    return $args;
  }
  // pre select all terms
  $args['selected_cats'] = array();
  foreach ($terms as $term) {
    $args['selected_cats'][] = $term->term_id;
  }
  return $args;
}    
add_filter('wp_terms_checklist_args', 'auto_check_hierarchial_terms_on_new_post', 10, 2);

There's a note on the filter, that it seems to be obsolete for WP 5 block editor.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744708552a4589200.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信