0

I'm trying to change the category for all posts of an author. I've been looking on here for about an hour and haven't found a solution..

Any help would be greatly appreciated :)

Thanks, j03

  • And what did you try so far ? – Sumit Apr 27 '16 at 18:01
  • I've searched through the posts seeing if they are in sequential order, they aren't. Was going to try a sequential statement to change them all at once.. Trying this route but there isn't a lot of documentation on it. – j03 Apr 27 '16 at 18:28
0

You could do this with a simple script. IMPORTANT! - Backup your database before doing this & attempt at your own risk.

Create a file called changeauthorcat.php in your main WordPress folder.

<?php

include( 'wp-config.php' );

global $wpdb;

// Author username
$username = 'exampleuser';
// New category slug
$newCatSlug = 'examplecategory';

// find the right author
$author = get_user_by( 'login', $username );
// find the right category
$category = get_category_by_slug( $newCatSlug );

if ( $author && $category ) {

    $posts = get_posts( array(
        'numberposts' => -1,
        'author' => $author->ID
        )
    );
    if ( $posts ) {
        foreach ( $posts as $post ) {
            // update all categories
            // the last parameter of false will replace all existing categories
            // see https://codex.wordpress.org/Function_Reference/wp_set_post_categories
            wp_set_post_categories( $post->ID, array( $category->term_id ), false );
        }
    }

}

?>

Change "exampleuser" to the login of the author you want to change. Change "examplecategory" to the slug of the new category.

Navigate to http://yourdomain.com/changeauthorcat.php. Delete the file when you're done.

Note that this will remove every other category from these posts and only place them in the new category. Using the wp_set_post_categories function will update the database with the correct category counts as well. Read more at: https://codex.wordpress.org/Function_Reference/wp_set_post_categories

0

You can get author posts id's

ids =[]
ids = SELECT id FROM `wp_posts` WHERE `post_author`=1

and then you can loop result

new_category_id = 999
old_category_id = 111
for id in ids:
    UPDATE `wp_term_relationships` SET `term_taxonomy_id`=new_id WHERE `object_id`=id AND `term_taxonomy_id`=old_category_id

    or

    wp_set_post_categories( $post_ID, $post_categories, $append )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.