1

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 1

1
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 );
}
4
  • 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
  • 1
    Edited! I added 'name' instead 'names' at $args. For existing posts, just press the Update post button.
    – Luan Cuba
    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
    Snippet, run it once.
    – Luan Cuba
    Sep 8, 2017 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.