简体   繁体   English

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

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

I want to delete all that posts programmatically which are older than two years on my WordPress site. 我想以编程方式删除我的WordPress网站上超过两年的所有帖子。 there are around 37000 posts. 大约有37000个帖子。 need help to delete it in bulk. 需要帮助将其批量删除。 how it is possible.? 怎么可能呢? what will be the easiest way to delete.? 最简单的删除方法是什么?

The direct bulk method might be to speak some raw SQL ( DELETE FROM ... ), but unless you have taken the time to learn WordPress internals, I would not suggest this route. 直接的批量方法可能是讲一些原始的SQL( DELETE FROM ... ),但是除非您花时间学习WordPress的内部知识,否则我不建议您采用这种方法。 Instead -- as with many tasks WordPress -- look for a plugin. 而是像许多WordPress任务一样,寻找插件。 Consider this search , which presents Bulk Delete as the top options. 考虑此搜索该搜索将“ 批量删除”显示为最重要的选项。

Indeed, Bulk Delete is a plugin we have used in office for just such an occurrence. 确实,批量删除是我们在办公室中经常使用的插件。

Good luck! 祝好运!

运行此SQL查询

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

Try with this code.. Please read my comments in the code for more understanding. 尝试使用此代码。请阅读我在代码中的注释以获取更多理解。 Function will go into your functions.php 函数将进入您的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');

You might just like this plugin (which I wrote) Auto Prune Posts, it will delete posts after , say 2 years, based on taxonomy + age. 您可能只是喜欢这个插件(我写的)“自动修剪帖子”,它会根据分类法+年龄在2年后删除帖子。 https://wordpress.org/plugins/auto-prune-posts/ https://wordpress.org/plugins/auto-prune-posts/

When you just "DELETE FROM .. " you WILL end up with orphaned data in at least the postmeta table. 当您只是“从...删除”时,您将至少在postmeta表中得到孤立的数据。 I think you do not want that. 我想你不想那样。

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

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