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 badges1 Answer
Reset to default 0For 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条)