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

categories - Automated adding of one tag to all the posts in a category

programmeradmin1浏览0评论

I'm currently redesigning a site that has 700+ posts, divided into about 150 different categories. Some categories have 2 posts in them, other categories have 70.

Now for every category, I want to add a tag to all the posts in that category.

Does anybody know if there is a way to automatically add a specific tag to all the posts in a category?

Or is the only way going through all the posts individually and adding a tag to each one, one by one?

Any help much appreciated!

I'm currently redesigning a site that has 700+ posts, divided into about 150 different categories. Some categories have 2 posts in them, other categories have 70.

Now for every category, I want to add a tag to all the posts in that category.

Does anybody know if there is a way to automatically add a specific tag to all the posts in a category?

Or is the only way going through all the posts individually and adding a tag to each one, one by one?

Any help much appreciated!

Share Improve this question asked Oct 19, 2011 at 17:08 SqurlerSqurler 3951 gold badge4 silver badges18 bronze badges 1
  • Thanks for pointing that out. I've revisited past questions and added comments where appropriate. – Squrler Commented Oct 21, 2011 at 2:11
Add a comment  | 

1 Answer 1

Reset to default 2

A good starting point is (as always) the Codex function reference.

There's one nice little function that can be used to add tags or categories (or whatever built-in or custom taxonomy) to a post: wp_set_object_terms();. Maybe wp_set_post_terms(); is more easy to use. Further explanation can be found when you follow the link.

There are multiple ways to do this: Bulk adding with fetching all posts (for ex. from a category) with get_posts(); (or other queries), then looping through all given post objects and adding the needed tags. You could also try to add bulk editing options to your admin UI, but in your case I'd go the easiest way and just write a nice little function that adds your tags to one category and another one to check if it was added. Then proceed to the next category. Writing some custom function that catches all your added tags (true/false) and the check afterwards wouldn't be much. Dumping this into a nice table on shutdown wouldn't hurt:

function wpse31531_add_tags()
{
    global $_dump_tag_check;

    // the following array can contain any taxonomy. But you have to name it inside wp_set_post_terms. If you need to add tags, use 'post_tag' as 3rd argument.
    $tags = array( 'YOUR TAG NAME A HERE', 'YOUR TAG NAME B HERE' );

    $args = array( 'post_type' => 'post', 'numberposts' => -1, 'post_status' => published );
    $posts = get_posts( $args );
    foreach ( $posts as $post )
    {
        $check = wp_set_post_terms( intval( $post->ID ), $tags, 'post_tag', true ); // set last value to false if you want to replace existing tags.
        $_dump_tag_check[ $post->ID ]['tags'] = $check ? explode( ", ", $tags ) : 'failed';
    }
}
add_action( 'init', 'wpse31531_add_tags' );


function wpse31531_dump_tag_check()
{
    echo '<table><thead><tr><th>Post ID</th><th>Tags</th></tr></thead><tbody>';
    foreach ( $GLOBALS['_dump_tag_check'] as $ID => $check )
        echo "<tr><td>{$ID}</td><td>{$check['tags']}</td></tr>";
    echo '</tbody></table>';
}
add_action( 'wpse31531_dump_tag_check', 'shutdown' );

When you read through linked codex pages, you'll see that above code won't work out of the box and is more like a rough guide. It would be highly appreciated if you can edit my answer with your working code for later readers. Thanks.

发布评论

评论列表(0)

  1. 暂无评论