首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何更改批量Wordpress帖子状态?

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

Stack Overflow用户
提问于 2018-04-08 04:28:57
回答 1查看 65关注 0票数 0

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

代码语言:javascript
运行
复制
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 );  
    }
}
EN

回答 1

Stack Overflow用户

发布于 2018-04-08 15:24:50

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

代码语言:javascript
运行
复制
    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 );
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49711678

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档