代码之家  ›  专栏  ›  技术社区  ›  Cray

30天后丢弃自定义帖子类型

  •  0
  • Cray  · 技术社区  · 7 年前

    为此,我从@pieter goosen那里找到了一个很好的解决方案,可以在几天后删除帖子: https://wordpress.stackexchange.com/questions/209046/delete-expired-posts-after-a-number-of-days-after-they-expired

    我的问题是,该函数删除此自定义帖子类型的所有帖子,并且不使用垃圾桶。

    我的代码如下所示:

    function get_exired_posts_to_delete()
    {
        /**
         * If you need posts that expired more than a week ago, we would need to
         * get the unix time stamp of the day a week ago. You can adjust the relative
         * date and time formats as needed.
         * @see http://php.net/manual/en/function.strtotime.php
         * @see http://php.net/manual/en/datetime.formats.php
         */
        // As example, we need to get posts that has expired more than 7days ago
        $past = strtotime( "- 1 week" );
    
        // Set our query arguments
        $args = [
            'fields'         => 'ids', // Only get post ID's to improve performance
            'post_type'      => 'job',
            'post_status'    => 'publish',
            'posts_per_page' => -1,
            'date_query'    => array(
                'column'  => 'post_date_gmt',
                'before'   => '30 days'
            )
        ];
        $q = get_posts( $args );
    
        // Check if we have posts to delete, if not, return false
        if ( !$q )
            return false;
    
        // OK, we have posts to delete, lets delete them
        foreach ( $q as $id )
            wp_delete_post( $id );
    }
    
    // expired_post_delete hook fires when the Cron is executed
    add_action( 'expired_post_delete', 'get_exired_posts_to_delete' );
    
    // Add function to register event to wp
    add_action( 'wp', 'register_daily_post_delete_event');
    function register_daily_post_delete_event() {
        // Make sure this event hasn't been scheduled
        if( !wp_next_scheduled( 'expired_post_delete' ) ) {
            // Schedule the event
            wp_schedule_event( time(), 'daily', 'expired_post_delete' );
        }
    }
    

    有没有更好的解决方案来使用服务器cron而不是WP cron?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Cray    7 年前

    我找到了问题的解决方案。

    对于垃圾的问题,我将参数wp\u delete\u post()更改为wp\u trash\u post(),因为wp\u delete\u post()仅适用于本机帖子、页面和附件。@rarst的回答很好: https://wordpress.stackexchange.com/questions/281877/error-after-deleting-custom-post-type-with-a-function-no-trash-used/281888#281888

    这是我的代码:

    function get_delete_old_jobs() {
        // Set our query arguments
        $args = [
            'fields'         => 'ids', // Only get post ID's to improve performance
            'post_type'      => 'job',
            'post_status'    => 'publish',
            'posts_per_page' => -1,
            'date_query'    => array(
                'before' => date('Y-m-d', strtotime('-30 days'))
            )
        ];
        $q = get_posts( $args );
    
        // Check if we have posts to delete, if not, return false
        if ( !$q )
            return false;
    
        // OK, we have posts to delete, lets delete them
        foreach ( $q as $id )
            wp_trash_post( $id );
    }
    
    // expired_post_delete hook fires when the Cron is executed
    add_action( 'old_job_delete', 'get_delete_old_jobs' );
    
    
    // Add function to register event to wp
    add_action( 'wp', 'register_daily_jobs_delete_event');
    
    function register_daily_jobs_delete_event() {
        // Make sure this event hasn't been scheduled
        if( !wp_next_scheduled( 'old_job_delete' ) ) {
            // Schedule the event
            wp_schedule_event( time(), 'hourly', 'old_job_delete' );
        }
    }