WordPress.org

Ready to get started?Download WordPress

Forums

Special way to assign posts to categories based on posts' tag. (2 posts)

  1. fixxi.net
    Member
    Posted 1 year ago #

    I am wondering if someone can suggest a way or a script where posts can be assigned to categories according to the posts's tags. However I need to do this in bulk for multiple posts.

    For example, if a post has these tags: nice, great and orange I need after having ran the script the post to end up being assigned to categories with names: nice, great and orange. This can assume that each corresponding category already exists.

  2. bcworkz
    Member
    Posted 1 year ago #

    Query for whichever posts you want to process, possibly all of them. Then loop through all the posts, much like as done on the main page, except in this case you use wp_get_post_tags(). Process the array returned in order to supply arguments for wp_set_post_terms() which will add the categories for each post. You can apply any extra logic to skip certain tags or whatever. Repeat for every post returned from the query.

    In the future, you could capture the proper post status transition to get new and updated posts and use a similar script to add categories based on tags for the post. Thus you would only need to run the bulk script once.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.