0

I need to retrieve a list of posts that are in trash based on the date on which they were trashed.

The following code allows me to retrieve posts that are in trash, but how to filter them by date?

    $options = array(
        'post_status' => 'trash',
        'post_type'   => 'post',
        'nopaging'    => TRUE
    );

    $wp_query = new WP_Query();
    $posts    = $wp_query->query( $options );
4
  • Did you try the date_query parameter?
    – birgire
    Sep 2, 2014 at 11:12
  • That will allow me to retrieve posts based on created date or modified date. But how to use trashed date with date_query?
    – Sudar
    Sep 2, 2014 at 11:15
  • modified date will be the date that WP trashed the post :) Sep 2, 2014 at 11:17
  • @TheDeadMedic I have not tried the code yet, but when I look in the admin UI the Last Modified date is not updated when a post is trashed.
    – Sudar
    Sep 2, 2014 at 11:24

1 Answer 1

5

When post is trashed, WP creates a meta field named _wp_trash_meta_time and it saves trashed time in that field. You can use that field to get trash time.

Edit:

Example:

$args = array(
    'post_status' => 'trash',
    'post_type'   => 'post',
    'nopaging'    => TRUE,
    'meta_query' => array(
        array(
            'key'     => '_wp_trash_meta_time',
            'value'   => array( strtotime("last week"), date('U') ),
            'compare' => 'BETWEEN',
        ),
    ),
);

Using this, you can get trashed posts which are trashed in last week.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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