I want to load posts (product type) of all the products that have custom-field ('date_flag'), if the time that stored on this custom field is before more than 4 hours (if 4 hours passed from the date that stored on this custom field).
For example, if 'date_flag' is 2020-11-12 10:00:00 and now its 2020-11-12 14:30:00 then give me this post.
$args= array(
'post_type' => 'product',
'posts_per_page' => 5,
'meta_query' => array(
array(
'key' => 'date_flag',
'value' => what-to-do-here
'compare' => what-to-do-here
),
My Answer:
'product',
'meta_query' => array(
array(
'key' => 'date_flag',
'value' => date('Y-m-d H:i:s', strtotime('-4 hours')),
'compare' => '<=',
'type' => 'DATETIME'
)
)
);
$query = new WP_Query($args);
This code snippet creates a WP_Query object to fetch posts of the 'product' post type that have a 'date_flag' custom field value that is 4 hours or more in the past. The 'value' parameter is set to the current date and time minus 4 hours, and the 'compare' parameter is set to '<=' to check if the custom field value is less than or equal to the specified value. The 'type' parameter is set to 'DATETIME' to ensure proper comparison of datetime values.
Rate this post
3 of 5 based on 8856 votesComments