mysql - 从垃圾箱中删除帖子,为其添加自定义字段

标签 mysql wordpress function

对于垃圾箱中的所有帖子,我需要执行以下操作:

1) 将其状态从“垃圾”更改为“发布”

2) 将名为“new_status”的自定义字段添加到那些值为“已删除”的帖子。

理想情况下,我需要使用一个可以放置在functions.php中的函数。

我想我需要从这样的事情开始......

$myconversion = $wpdb->get_row("SELECT * FROM 'wp_posts' WHERE post_status = 'publish'");

...但是MYSQL不是我的强项。

任何帮助都会很棒。

最佳答案

幸运的是,你不必了解 SQL,你有强大的 WP_Query 函数,所以我认为你需要这样的东西(将其添加到你的functions.php文件中)

function wp_update_all_posts_status($params = null){
    $args = array(
        'nopaging' => true, // Loop through all posts at once
        'post_status' => array('trash'), //Get posts "from the trash"
    );

    $the_query = new WP_Query( $args );
    if ( $the_query->have_posts() ) {

        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            // Change the status the post to publish
            $updated = wp_update_post( array('ID' => $the_query->post->ID, 'post_status' => 'publish' ));
            //add the post "new_status" meta
            add_post_meta( $the_query->post->ID, 'new_status', 'deleted' ); 
        }
    }
}

wp_update_all_posts_status();

关于mysql - 从垃圾箱中删除帖子,为其添加自定义字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36573954/

相关文章:

php - 使用 PHP 和 MySQL 在 Android 中删除用户

javascript - 展平数组 : where does the 0 come from?

mysql - AWS RDS 手动快照是否增量存储?

php - 只允许在文本区域中换行

mysql - 如何将电话号码调整为相同格式?

php - 如何仅在 WordPress 中运行主题激活功能?

javascript - 来自 WordPress 的 YQL rss feed 设置限制

php - 您可以在 Wordpress 上添加自定义仪表板 Logo 吗?

javascript - 循环内的 JS 函数 - 内存/CPU 注意事项

c++ - 是否有任何理由将 Lambda 包装在命名函数中?