Skip to content
Advertisement

Query post types based on timestamp

So I have a method where I’m setting a value in the database as a timestamp, so this property:

'azure_picture_expiration' => $profile->set_azure_picture_expiration(strtotime('+7 days')),

Sets the following timestamp for example 1605047781, which is GMT: Tuesday, November 10, 2020 10:36:21 PM (So 7 days from today).

What I’m attempting to achieve:

I have the following WP_Query that queries through my profile post type, and looks for all the azure_picture_expiration and azure_picture_big_expiration values.

How could I query through and pull in all profiles that will expire in 2 days? Is it possible to query through and pull profiles that will expire within 2 days?

Here is my query:

$profiles = Profile::get([
    'post_status' => [
        'publish', 'draft', 'pending', 'private'
    ],
    'posts_per_page' => 4,
    'meta_query' => [
        'relation' => 'OR',
        [
            'key' => 'azure_picture_expiration',
            'value' => '',
            'compare' => '',
        ],
        [
            'key' => 'azure_picture_big_expiration',
            'value' => '',
            'compare' => '', 
        ],
    ],
]);

All help would be appreciated, I don’t have the compare filled in, because I wasn’t sure how to determine the time.

Advertisement

Answer

This seemed to work, please let me know if you have a better method:

$profiles = Profile::get([
    'post_status' => [
        'publish', 'draft', 'pending', 'private'
    ],
    'posts_per_page' => -1,
    'meta_query' => [
        'relation' => 'OR',
        [
            'key' => 'azure_picture_expiration',
            'value' => strtotime('+2 days'),
            'compare' => '<=',
        ],
        [
            'key' => 'azure_picture_big_expiration',
            'value' => strtotime('+2 days'),
            'compare' => '<=',
        ],
    ],
]);
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement