admin管理员组文章数量:1417104
Is there a way to filter Wordpress save_post in a way that if more than one category is present, the Uncategorized category is unset/removed? This should works also on first post save
Is there a way to filter Wordpress save_post in a way that if more than one category is present, the Uncategorized category is unset/removed? This should works also on first post save
Share Improve this question asked Jan 31, 2017 at 17:14 RiccardoRiccardo 9711 gold badge18 silver badges37 bronze badges2 Answers
Reset to default 5Yes. You can use the save_post action and do it here is some function that remove the default wordpress category if there is some other category selected.
I added some comments so you will understand the process.
function remove_uncategorized($post_id) {
// get default category
$default_category = (int)get_option('default_category');
// check if the post is in the default category
if(in_category($default_category, $post_id)) {
// get list of all the post categories
$post_categories = get_the_category($post_id);
// count the total of the categories
$total_categories = count($post_categories);
// check if the post is in more than 1 category (the default one and more..)
if($total_categories > 1) {
// remove the default category from the post
wp_remove_object_terms($post_id, $default_category, 'category');
}
}
}
add_action( 'save_post', 'remove_uncategorized' );
Shibi's answer no longer works properly when saving posts with the new block editor (Gutenberg) because the call to set the post terms (which sets the categories) now occurs after the save_post
action is run. Instead, the function in the accepted answer checks only the old terms, meaning only the second time after a post is saved with the default category does it get removed. See the WP_REST_Post_Controller
's update_item
function (comments mine):
public function update_item( $request ) {
// First the function updates the post. The `save_post` action is run,
// but it doesn't yet see the new categories for the post.
$post_id = wp_update_post( wp_slash( (array) $post ), true );
...
// Only later does the block editor set the new post categories.
$terms_update = $this->handle_terms( $post->ID, $request );
...
}
I achieved the required behaviour in a way that works on both the classic and block editor by hooking in to set_object_terms
, which occurs in the block editor as part of its call to $this->handle_terms
:
/**
* Remove unnecessary "Uncategorised" category on posts saved with another, non-default
* category.
*
* This is performed on the `set_object_terms` action as part of `wp_set_object_terms` function
* because the `save_post` action, where this would logically be run, is run *before* terms are
* set by the block editor (in contrast to the classic editor).
*
* @param int $object_id Object ID.
* @param array $terms An array of object terms.
* @param array $tt_ids An array of term taxonomy IDs.
* @param string $taxonomy Taxonomy slug.
*/
function wpse_254657_remove_superfluous_uncategorised( $object_id, $terms, $tt_ids, $taxonomy ) {
if ( 'category' !== $taxonomy ) {
return;
}
$post = get_post( $object_id );
if ( is_null( $post ) || 'post' !== $post->post_type ) {
return;
}
if ( count( $terms ) <= 1 ) {
return;
}
// Get default category.
$default_category = get_term_by( 'id', get_option( 'default_category' ), $taxonomy );
// Rebuild list of terms using $tt_ids and not the provided $terms, since
// $terms can be mixed type and is unsanitised by `wp_set_object_terms`.
$terms = array();
foreach( $tt_ids as $tt_id ) {
$term = get_term_by( 'term_taxonomy_id', $tt_id, $taxonomy );
if ( $term ) {
$terms[] = $term;
}
}
if ( ! in_array( $default_category->term_id, wp_list_pluck( $terms, 'term_id' ), true ) ) {
return;
}
// Remove the default category from the post.
wp_remove_object_terms( $post->ID, $default_category->term_id, 'category' );
}
add_action( 'set_object_terms', 'wpse_254657_remove_superfluous_uncategorised', 10, 4 );
In the above, I had to also build my own list of $terms
because the one provided by the hook can contain mixes of strings and ints or an array of mixed strings and ints. It's therefore simpler to get terms this way. With the term caching WordPress does, this shouldn't add much overhead.
本文标签: categoriesRemoving Uncategorized on post save if other category present
版权声明:本文标题:categories - Removing Uncategorized on post save if other category present? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745253691a2649974.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论