WordPress批量类别添加到MySQL的所有帖子

时间:2013-05-10 13:39:12

标签: php mysql sql wordpress bulk

我必须将所有旧帖子添加到新创建的类别并保留类别帖子。我已经尝试通过wordpress编辑按钮,但它只在每页20个帖子,并且还有超过100页。所以我想知道是否有一个我可以执行的MySQL片段并立即将这个类别添加到帖子中???

1 个答案:

答案 0 :(得分:0)

这里是代码

<?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;
?>

代码在所有帖子和页面上运行循环,并检查类别ID是否未添加到帖子或页面中。如果没有添加,请将类别ID添加到帖子或页面。