如何更改批量Wordpress帖子状态?

时间:2018-04-07 20:28:57

标签: php wordpress post

是否可以使用SQL或功能代码发布所有草稿帖子?我尝试在我的函数文件中使用此代码,但不起作用。

add_action('admin_init','wpse_244394');

function wpse_244394(){
    $args = array('post_type'=> 'post',
         'post_status' => 'draft',
         'posts_per_page'=>-1
    );
    $draft_posts = get_posts($args);

    foreach($draft_posts as $post_to_publish){
       $query = array(
        'ID' => $post_to_publish->ID,
        'post_status' => 'publish',
       );
       wp_update_post( $query, true );  
    }
}

1 个答案:

答案 0 :(得分:0)

尝试使用“wp_publish_post($ post_id)”而不是“wp_update_post”。

    add_action('admin_init','wpse_244394');

    function wpse_244394(){
    $args = array('post_type'=> 'post',
        'post_status' => 'draft',
        'posts_per_page'=>-1
    );
    $draft_posts = get_posts($args);

    foreach($draft_posts as $post_to_publish){
        wp_publish_post( $post_to_publish->ID );
    }
}