Using WordPress ‘bulk_edit_posts()’ PHP function

The bulk_edit_posts() WordPress PHP function updates all bulk edited posts or pages, adding (but not removing) tags and categories. It specifically avoids processing pages when they would be their own parent or child.

Usage

Here’s a basic example of how you can use bulk_edit_posts(). Assume you have an array of post data, $post_data, that you want to process:

$post_data = array(
    'post_type' => 'post',
    'tags_input' => array('tag1', 'tag2'),
    'post_category' => array(1, 2)
);
bulk_edit_posts($post_data);

In this example, bulk_edit_posts($post_data) will process all the posts, adding ‘tag1’ and ‘tag2’ as tags, and adding them to categories 1 and 2.

Parameters

  • $post_data (array|null) (Optional): The array of post data to process. Defaults to the $_POST superglobal. Default: null

More information

See WordPress Developer Resources: bulk_edit_posts

Examples

Adding Categories to Multiple Posts

This code snippet adds categories to multiple posts.

$post_data = array(
    'post_type' => 'post',
    'post_category' => array(1, 2, 3)
);
bulk_edit_posts($post_data);

Adding Tags to Multiple Posts

This code snippet adds tags to multiple posts.

$post_data = array(
    'post_type' => 'post',
    'tags_input' => array('tag1', 'tag2', 'tag3')
);
bulk_edit_posts($post_data);

Adding Categories and Tags to Multiple Posts

This code snippet adds both categories and tags to multiple posts.

$post_data = array(
    'post_type' => 'post',
    'post_category' => array(1, 2, 3),
    'tags_input' => array('tag1', 'tag2', 'tag3')
);
bulk_edit_posts($post_data);

Adding Categories to Pages

This code snippet adds categories to multiple pages.

$post_data = array(
    'post_type' => 'page',
    'post_category' => array(1, 2, 3)
);
bulk_edit_posts($post_data);

Adding Tags to Pages

This code snippet adds tags to multiple pages.

$post_data = array(
    'post_type' => 'page',
    'tags_input' => array('tag1', 'tag2', 'tag3')
);
bulk_edit_posts($post_data);