【问题标题】:WordPress custom Post ExpiryWordPress 自定义过期时间
【发布时间】:2022-01-20 22:59:05
【问题描述】:

我的代码功能有问题。自定义帖子应在一定天数后过期。目前设置为 3 天,以便我进行测试。

但是即使过期日期早于今天的日期,帖子也会过期,我不知道为什么?!

// Setup Cron Job Function to expire posts and send out emails.
function listing_expiry_date() {

    // Get the current date
    date_default_timezone_set('Europe/London');
    $today = date('m/d/Y');
                            
    // Custom Post Type for listings, grab published posts
    $args = array(
        'post_type'      => array( 'post_type_listings' ),
        'post_status'    => array( 'Publish' ),
        'posts_per_page' => -1,
    );
    
    // The Query
    $query = new WP_Query( $args );
    
    // The Loop
    if ( $query->have_posts() ) :
        while ( $query->have_posts() ) :
            $query->the_post();
            
            // Get the listing ID
            $post_id = get_the_ID();
                
    // Get listing post published date (not pending date)    
    $published_date = get_the_date( 'm/d/Y', get_the_ID() );
    
                                                    
        $expiry_date  = date( 'm/d/Y', strtotime( '+3 days', strtotime($published_date) ) );        
            
        // If todays date is equal to or greater than the expiry date...
        if ( ($today == $expiry_date) || ($today > $expiry_date) ) :

    // change post status to 'expired'
    $postdata = array(
    'ID'          => $post_id,
    'post_status' => 'expired',

        );

    // Update post data
    wp_update_post($postdata);
                                                                                
    endif;                          
            
    endwhile; //endwhile The Loop
endif; //endif The Loop         
    
}

【问题讨论】:

    标签: php wordpress date


    【解决方案1】:

    您的问题似乎是您如何评估日期。在这种情况下,最好使用 Unix 时间戳来评估哪个更大。

    function listing_expiry_date() {
    
        // Get the current date.
        date_default_timezone_set( 'Europe/London' );
        $today = date( 'U' );
    
        // Custom Post Type for listings, grab published posts.
        $args = array(
                'post_type'      => array( 'post_type_listings' ),
                'post_status'    => array( 'publish' ),
                'posts_per_page' => -1,
        );
        // The Query.
        $query = new WP_Query( $args );
    
        // The Loop.
        if ( $query->have_posts() ) :
            while ( $query->have_posts() ) :
                $query->the_post();
    
                // Get the listing ID.
                $post_id = get_the_ID();
    
                $expiry_date = date( 'U', strtotime( '+3 days', get_the_date( 'U', $post_id ) ) );
                // If Expiration date is < or = today.
                if ( $expiry_date <= $today ) :
                    // change post status to 'expired'
                    $postdata = array(
                            'ID'          => $post_id,
                            'post_status' => 'expired',
    
                    );
    
                    // Update post data.
                    wp_update_post($postdata);
    
                endif;
    
            endwhile; // endwhile The Loop.
        endif; // endif The Loop.
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多