如何使用帖子元、帖子附件和文件删除一年以上的帖子?

时间:2017-03-27 作者:Grandeto

有人能帮我删除一年以上带有Posteta、附件和文件的帖子吗?

Tnx公司

2 个回复
SO网友:Md. Mrinal Haque

当我在cron 相关项目
1。安装并激活[WP Crontol plugin][1]2、进入工具->Cron事件。3、在“添加PHP cron事件”选项卡上创建新的cron作业。在这里,我使用PHP代码

global $wpdb;
$posts_table = $wpdb->prefix . \'posts\';
$term_relationships_table = $wpdb->prefix . \'term_relationships\';
$postmeta_table = $wpdb->prefix . \'postmeta\';

$sql = "DELETE a, b, c
FROM $posts_table a
LEFT JOIN $term_relationships_table b ON (a.ID = b.object_id)
LEFT JOIN $postmeta_table c ON (a.ID = c.post_id)
WHERE a.post_type = \'post\'
AND DATEDIFF( NOW(), a.post_date) > 365
";

$wpdb->query( $wpdb->prepare($sql) );

now12:00:00 在里面Next Run
Once Hourly 在里面Recurrence

保存此cron事件并单击立即运行

SO网友:Serkan Algur

我已经回答了这类问题here 但您要删除post_meta 也这是您的代码。请在上测试此localhost 第一

<?php

// Please use this function carefully.
// Changes can\'t undone. Best regards from Serkan Algur :)
// Let the function begin
function delete_oldest_posts_salgur()
{
    // We will collect posts from two years ago :)
    $args = array(
        \'date_query\' => array(
            array(
                \'column\' => \'post_date_gmt\',
                \'before\' => \'2 years ago\', // change this definition for your needs
            ),
        ),
        \'posts_per_page\' => -1,
    );
    // Get posts via WP_Query
    $query = new WP_Query($args);
    //We are doing this for \'foreach\'
    $posts = $query->get_posts();

    foreach ($posts as $post) {
        echo $post->ID;
        $args = array(
                \'posts_per_page\' => -1,
                \'order\'          => \'ASC\',
                \'post_mime_type\' => \'image\', // only for images. Look at https://codex.wordpress.org/Function_Reference/get_children
                \'post_parent\'    => $post->ID,
                \'post_type\'      => \'attachment\',
            );

        $attachments = get_children($args);
        $post_metas = get_post_meta($post->ID); // Get all post metas

        if ($post_metas) {
            foreach ($post_metas as $key => $pmeta) { //delte all of them
                delete_post_meta($post->ID, $key);
            }
        }

        if ($attachments) {
            foreach ($attachments as $attachment) {
                wp_delete_attachment($attachment->ID, true); //If You Want to trash post set true to false
            }
        }
        wp_delete_post($post->ID, true); //If You Want to trash post set true to false
    }
}

// Problem Solver Cron Job definition
function cron_delete_oldest_posts_salgur()
{
    if (! wp_next_scheduled(\'delete_oldest_posts_salgur\')) {
        wp_schedule_event(current_time(\'timestamp\'), \'daily\', \'delete_oldest_posts_salgur\');
    }
}
add_action(\'wp\', \'cron_delete_oldest_posts_salgur\');

相关推荐

Calculations in functions.php

我为自己创建了一个发票主题,并试图在自定义列中获取发票总额。我已经尝试调整模板页面中使用的代码,以便在函数中显示这一点。php文件,但它不工作。我的乘法操作数出现了一个操作数错误,我不知道为什么。这是我的代码(如有任何帮助,将不胜感激)。if( $column_name == \'invoice_total\' ) { $hours = 0; // This allows counting of each sub_field //* Set repeater va