如何将wordpress所有文章批量改为已发布状态

2025-03-03 18:49:50
推荐回答(2个)
回答1:

function wpdit_update_post_status() {
//第一步:获取所有post的ID

// 获取所有post_type
$post_types = array();
foreach ( get_post_types( array('public' => true)) as $post_type => $value ) {
if($post_type != 'page' && $post_type != 'attachment'){
$post_types[] = $post_type;
}
}
// 如果只想指定默认的post,那直接 $post_types = 'post' 就行
$post_types = '"'.implode('","', $post_types).'"';

$post_ids = $wpdb->get_col('SELECT ID FROM '.$wpdb->posts.' WHERE post_password="" AND post_status="publish" AND post_type in('.$post_types.') ORDER BY ID DESC');

if ( $post_ids ){
//进行发布状态的更新,除非必要,否则不建议这样操作
global $wpdb;

$result = array();
$post_status = 'publish';
// 每次只提100篇日志进行更新,避免全部日志塞到内存中。
while ( $next_posts = array_splice( $post_ids, 0, 100 ) ) {

foreach ($next_posts as $post_ID ) {

$where = array( 'ID' => $post_ID )
$updated = $wpdb->update( $wpdb->posts, compact('post_status'), $where );
$result[$post_ID] = $updated;

if ( ! $updated ){
continue;
}
}
}
var_dump($result); //执行完后输出结果
}
}

回答2:

global $wpdb; // 筛选所有状态为草稿、定时发布的文章ID $ids = $wpdb->get_col("Select ID from $wpdb->posts where $wpdb->posts.post_type = 'post' and $wpdb->posts.post_status in ('draft','future')"); $result = array(); if ( count($ids) ) : foreach ($ids as $key => $post_id) { $result[$post_id] = $wpdb->update( $wpdb->posts, array('post_status' => 'publish' ), array('ID' => $post_id) ); clean_post_cache( $post_id ); } endif; var_dump($result);代码仅作为参考,可实现将所有状态为草稿、定时发布的文章批量改成已发布状态。这个代码需谨慎执行,建议执行前将数据库备份。