繁体   English   中英

删除Wordpress网站中2年以上的帖子

[英]Delete posts older than 2 years in wordpress site

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

直接的批量方法可能是讲一些原始的SQL( DELETE FROM ... ),但是除非您花时间学习WordPress的内部知识,否则我不建议您采用这种方法。 而是像许多WordPress任务一样,寻找插件。 考虑此搜索该搜索将“ 批量删除”显示为最重要的选项。

确实,批量删除是我们在办公室中经常使用的插件。

祝好运!

运行此SQL查询

DELETE FROM `wp_posts` WHERE YEAR(CURDATE()) - YEAR(`post_date`) >= 2;

尝试使用此代码。请阅读我在代码中的注释以获取更多理解。 函数将进入您的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');

您可能只是喜欢这个插件(我写的)“自动修剪帖子”,它会根据分类法+年龄在2年后删除帖子。 https://wordpress.org/plugins/auto-prune-posts/

当您只是“从...删除”时,您将至少在postmeta表中得到孤立的数据。 我想你不想那样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM