简体   繁体   中英

WordPress Bulk category add to all posts with MySQL

I have to add all of my old posts to new created category and keeping the categories posts are in. I've tried doing it via wordpress edit button, but it only does on 20 posts per page, and there are more than 100 pages. So I was wondering if there's a MySQL snippet I could execute and add the category to the posts at once???

Ok here is the code

<?php
$args = array( 'numberposts' => -1, 'orderby' => 'post_date' );
$postslist = get_posts( $args );
foreach ($postslist as $post) :  setup_postdata($post);
$post_id = get_the_ID();

$cat_id = 99;
if ( !(in_category($cat_id)) ) {
    wp_set_object_terms( $post_id, array( $cat_id), 'category', false);
    echo 'No';
}else{

}
endforeach;
?>

The code is running a loop on all posts and pages and checking if category ID is not added into the post or page. if not added than add the category ID to the post or page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM