Is there a way to sync categories and tags? I want a way that every post will have a tag that is the same as the category or categories that its in.
1 Answer
add_action( 'save_post', 'add_tag_based_on_category' );
function add_tag_based_on_category( $post_id ) {
$args = array( 'fields' => 'names' );
$categories = wp_get_post_categories( $post_id, $args );
$cat_names = implode( ', ', $categories );
wp_set_post_tags( $post_id, $cat_names, true );
}
-
Nothing happened to existing posts, or to the new post I made with a category. I put it in the
functions.php
file, is that the wrong place? Sep 8, 2017 at 15:34 -
1Edited! I added 'name' instead 'names' at $args. For existing posts, just press the Update post button. Sep 8, 2017 at 15:47
-
Is there any way to emulate pressing the update button on all posts? Sep 8, 2017 at 15:49
-
1