最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论