我必须将我所有的旧帖子添加到新创建的类别中,并保持帖子所在的类别。我试过通过wordpress编辑按钮来做,但是每页只有20个帖子,而有100多个页面。所以我想知道是否有一个MySQL片段,我可以执行并添加类别到帖子一次?
下面是代码
<?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添加到文章或页面中。