admin管理员组

文章数量:1323029

I am looking for a way to assign (add) a category to a list of posts without editing each and every post - even by using bulk edit.

Why? Because doing it manually (even using bulk) will update the published time and I do not want that.

Any idea as to how this can be done? I understand that I need an array of post ID:s, which I have. But how do I do this using my theme's functions file?

I am looking for a way to assign (add) a category to a list of posts without editing each and every post - even by using bulk edit.

Why? Because doing it manually (even using bulk) will update the published time and I do not want that.

Any idea as to how this can be done? I understand that I need an array of post ID:s, which I have. But how do I do this using my theme's functions file?

Share Improve this question asked Sep 18, 2020 at 17:24 Jen Austin MillerJen Austin Miller 1
Add a comment  | 

1 Answer 1

Reset to default 0

You can set post terms programmatically with wp_set_object_terms(). There's also wp_set_post_terms() which is a wrapper for the first function, but with some extra processing of the parameters in it.

foreach ( $array_of_post_ids as $post_id ) {
    // appends my-category to a post's categories
    wp_set_object_terms( $post_id, 'my-category', 'category', true );
}

本文标签: categoriesAdd Catgory (assign) to Post Programmatically (without editing)