php - 删除 wordpress 站点中超过 2 年的帖子

我想以编程方式删除我的 WordPress 网站上超过两年的所有帖子。大约有 37000 个帖子。需要帮助才能批量删除它。怎么可能。?什么是最简单的删除方法?

最佳答案

尝试使用此代码。请阅读我在代码中的注释以获得更多理解。函数将进入您的 functions.php

function get_delete_old_post() {
    // WP_Query arguments
    $args = array(
        'fields'         => 'ids', // Only get post ID's to improve performance
        'post_type'      => array( 'post' ), //post type if you are using default than it will be post
        'posts_per_page' => '-1',//fetch all posts,
        'date_query'     => array(
                                    'column'  => 'post_date',
                                    'before'   => '-2 years'
                                )//date query for before 2 years you can set date as well here 
        );

    // The Query
    $query = new WP_Query( $args );

    // The Loop
    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post();
            //delete post code
            //wp_trash_post( get_the_ID() );  use this function if you have custom post type
            wp_delete_post(get_the_ID(),true); //use this function if you are working with default posts
        }    
    } else {
        // no posts found
        return false;

    }
    die();
    // Restore original Post Data
    wp_reset_postdata();

}

add_action('init','get_delete_old_post');

https://stackoverflow.com/questions/46598585/

相关文章:

Python 2.7 - 如何检查是否按下了 SHIFT-Key 或 CTRL+Key?

r - dplyr 中未显示 SD 值

julia - 为什么 Julia 闭包不复制数组?

php - 使用 Laravel 5.2 集合中的列名获取唯一值的计数

java - 使用 AssertJ,如何对列表的内容执行复杂的断言?

wpf - 字符串格式正值和负值以及条件颜色格式 XAML

sql - Oracle SQL - 从给定的字符串形成一个虚拟表以与另一个表连接

php - 使用 symfony 文件系统代替原生 PHP 的优势

c# - 蓝牙扫描C#

r - 在 R 中制作特定的分位数图