dongshi7433 2018-03-06 18:22
浏览 36

一段时间后自动删除wordpress媒体的功能

I'm writing what I thought was a simple series of functions to assign a "deletion date" to media, then auto-delete those expired media when the site is visited.

The post_meta for deletion date is set for the images via an online form when the images are uploaded (using formidable pro forms and the "after_create_entry" hook. I can confirm that the meta field is created successfully, and the deletion date is assigned properly (called 'mtp_deletiondate'. To test, I did a wp_query on a custom page and each image has a properly set deletion date.

Next, I wanted to run a function that checked for expired images (set to be 21 days after date of upload), and if they are expired, to delete them. I want to trigger this function whenever I access the admin, because I figure I get in there at least once a month to run updates, and it's a good time to clean out the old images. For this situation, I decided not to do a cron job since I can reliably visit the site once a month anyway.

The issue is the function doesn't seem to be triggering, and I don't know if it's because the function is broken or because I'm using the wrong action, or both. Some research tells me to use the admin_init, but I've also tried "wp", and "wp-footer". It might be that I'm just misunderstanding the process. My function (in my theme's functions.php):

function drick_delete_expired_uploads() {
// WP_Query arguments
$args = array (
    'post_status'    => 'any',
    'post_type' => array( 'Attachment' ),
    'posts_per_page' => -1,
    'meta_query' => array(
        array(
            'key' => 'mtp_deletiondate',
        ),
    ),
);

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

// The Loop
if ( $mediaquery->have_posts() ) {
    while ( $mediaquery->have_posts() ) {
        $mediaquery->the_post();
        date_default_timezone_set('America/Denver');
        $CurrentDate = date('Y-m-d h:i');
        $CurrentDateStr = strtotime($CurrentDate);
        $DeletionDate = get_post_meta( $post->ID, 'mtp_deletiondate', true );
        $DeletionDateStr = strtotime($DeletionDate);
        if ( isset($DeletionDateStr) ) {
            if ( $DeletionDateStr < $CurrentDateStr ) { 
                wp_delete_attachment( $post->ID, true );
            }
        }
    }
} else {
    // no posts found
} // IF HAVE POSTS 

// Restore original Post Data
wp_reset_postdata();
}
add_action('admin_init', 'drick_delete_expired_uploads');

If I save my functions.php, then reload the Wordpress dashboard, then check my media, the expired images are still there. HOWEVER, if I add this function to an actual page then visit the page, it does work. So I believe the function is doing what it's supposed to, it's just not getting triggered properly? I also added a quick wp_mail() to the function in my functions.php, and when I visited the admin it did trigger the email, so I guess the function is firing.

I would appreciate any insight, thank you!

  • 写回答

1条回答 默认 最新

  • dsgnze6572 2018-03-06 18:51
    关注

    So I think I figured it out, but I don't know that I have an explanation as to why it works. Essentially it looks like wp_delete_attachment wasn't working, but wp_delete_post DOES work. I've tested and confirmed with three additional images that the function auto delete is triggered when accessing the admin dashboard. I also changed the way the query arguments are structured by only querying the expired images, rather than querying ALL images that have a mtp_deletiondate meta then running a conditional statement within the query. Don't know if that's related or not. The final working function:

    function drick_delete_expired_uploads() {
    // WP_Query arguments
    date_default_timezone_set('America/Denver');
    $CurrentDate = date('Y-m-d h:i');
    $args = array (
        'post_status'    => 'any',
        'post_type' => array( 'Attachment' ),
        'posts_per_page' => -1,
        'meta_query' => array(
            array(
                'key' => 'mtp_deletiondate',
                'value' => $CurrentDate,
                'compare' => '<',
                'type' => 'DATE',
            ),
        ),
    );
    
    // The Query
    $mediaquery = new WP_Query( $args );
    
    // The Loop
    if ( $mediaquery->have_posts() ) {
        while ( $mediaquery->have_posts() ) {
            $mediaquery->the_post();
            wp_delete_post(get_the_ID());
        }
    } else {
        // no posts found
    } // IF HAVE POSTS 
    
    // Restore original Post Data
    wp_reset_postdata();
    }
    add_action('admin_init', 'drick_delete_expired_uploads');
    

    Still look forward to any feedback from someone in the know that can tell me why the previous version wasn't working (looking to learn here). Thanks!

    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端