0

I have a website where posts expire. Currently I do this manually but I would prefer todo this automatically. I have been struggling with this for a few months now and I thought I finally found the solution with wp_query() but it is not displaying the expected results, instead it is displaying the last 10 posts that were published.

This is the current code:

function expire_posts() {
    $post_id = get_the_ID();
    $getdate = get_post_meta($post_id, 'Expiry Date', true);
    $expirydate = date ('d F Y', strtotime ($getdate));
    $today = date( 'd F Y');
    $args = array (strtotime($expirydate) < strtotime($today));
    $query = new WP_Query($args);
?><ol><?php
    if( $query->have_posts() ) : while ( $query->have_posts() ) : $query- 
>the_post(); 
?>
        <li><?php the_title(); ?></li><?php
    endwhile; 
?></ol><?php
        wp_reset_postdata();
    endif;
}

I have also used the following code and it returned the same results:

function expire_posts() {
    $args = array(
    'meta_key'     => 'Expiry Date',
'meta_value'   => date( "d F Y" ), // change to how "event date" is stored
'meta_compare' => '<',
);
$query = new WP_Query( $args );
?><ol><?php
    if( $query->have_posts() ) : while ( $query->have_posts() ) : $query- 
>the_post(); 
?>
        <li><?php the_title(); ?></li><?php
    endwhile; 
?></ol><?php
        wp_reset_postdata();
    endif;
}

It should return two specific published posts where the expiry date has past.

  • And what does it mean that the post is expired? What should happen in such case? They shouldn't be displayed on site? – Krzysiek Dróżdż May 24 '18 at 9:50
  • The aim is to update the post status to expired so that it doesn't show up in the search. Before I get to this step I first want to make sure that it is returning the correct data. – Ragnaboy May 24 '18 at 10:14
  • OK, so if you really need to change its status, then you should use WP Cron to do it automatically. But... If the only aim is to stop them to show, then using pre_get_posts will be much easier way... Last question: What is the format of date in Expiry Date field? – Krzysiek Dróżdż May 24 '18 at 10:16
  • 'd F Y' so '01 June 2018'. It is to update the post_status and post_modified – Ragnaboy May 24 '18 at 10:20
  • OK, so there is no way to compare it using WP_Query. Can you change that format to YYYY-mm-dd or YYYYmmdd? – Krzysiek Dróżdż May 24 '18 at 10:37
2

You must change your date to an ordering like @ Krzysiek Dróżdż said or use meta_type options. I have tested with the string version.

An example of meta_type $args is listed below. See also MySQL time variants

/*
Plugin Name: Meta Filter
Plugin URI: https://wordpress.stackexchange.com/questions/304333
*/

// ?XDEBUG_SESSION_START=304333

function wpse_304333() {
    echo '<p>wpse_304333</p>';

    $args = array(
        'post_type'  => 'my-post-type',
        'meta_key'   => 'my-date-as-string-yyyy-mm-dd',
        'meta_value' => '2018-03-00', // before march 2018
        'meta_compare' => '>',
    );

    $query = new WP_Query( $args );

    $titles = '';
    while ( $query->have_posts() ) : $query->the_post();
        $titles .= '<br/>' . get_the_title();
    endwhile;
    echo $titles;
    wp_reset_postdata();
}

add_action( 'admin_notices', 'wpse_304333' );

uses an SQL containing something like

AND ( 
  ( wp_postmeta.meta_key = 'my-date-as-string-yyyy-mm-dd'
    AND wp_postmeta.meta_value < '2018-03-00' )
)

Or use meta_type like

$args = array(
    'post_type'  => 'my-post-type',
    'meta_key'   => 'my-date-key',
    'meta_value' => $date_value
    'meta_compare' => '>',
    'meta_type' => 'DATETIME'
);

which is SQL converted using CAST. You can run the SQL below to check what values are 'cast' properly.

SELECT meta_value, CAST(meta_value AS DATETIME)
  FROM wp_postmeta
 WHERE CAST(meta_value AS DATETIME) IS NOT NULL

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.