最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - Add Catgory (assign) to Post Programmatically (without editing)

programmeradmin1浏览0评论

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 );
}
发布评论

评论列表(0)

  1. 暂无评论