0

I am trying to perform the following query, which originates by the fact the post's 'date' and 'time' are entered as separate custom fields.

$args = array(
      'post_type' => 'some_kind_of_posts',
      'posts_per_page' => 3,
      'orderby' => 'meta_value_num',
      'meta_key' => strtotime('date'. ' ' .'time'),
      'order' => 'DESC',
      'meta_query' => array(
           array(
                'key' => strtotime('date'. ' ' .'time'),
                'value' => strtotime(date('H:i',time() + 3600*2))
                'compare' => '<',
                ),
      ),
);
$query = new WP_Query( $args );

In other words, I need to first combine 'date' and 'time', in order to select posts which are older than the current time. The latter is converted into Unix epoch timestamp (is this a good option ?).

I have elsewhere checked strtotime(get_field('date'). ' ' .get_field('time')) and strtotime(date('H:i',time() + 3600*2)) by echoing them, and obtained the expected Unix epoch time in seconds.

However, key-value comparison fails: I do not manage to select posts which are older than current time.

Many thanks for any hint on how to tackle the problem.

6
  • you have 2 custom fields, one for the date and other for the time?
    – Tomás Cot
    Commented Sep 14, 2014 at 21:22
  • Yes, indeed, they are separated.
    – mannaia
    Commented Sep 15, 2014 at 5:21
  • I apologize if I'm misunderstanding: can you not just use WP_Query's date_query arg, or is this for an event post type? codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters
    – Caleb
    Commented Sep 17, 2014 at 2:46
  • @Caleb I have managed to use meta_query with key='date' and value='Ymd'. That works. However, I'd need to go further, by doing comparisons that take into account also 'time'and not only 'date'.
    – mannaia
    Commented Sep 17, 2014 at 10:34
  • if the date and time stored in the post meta is the published date and time, then you can use WP_Query entirely; the date_query parameter includes time as well. take a look at the link above to the Codex... or am I misunderstanding what you're storing in the post meta?
    – Caleb
    Commented Sep 17, 2014 at 16:56

2 Answers 2

0

This piece of code should solve your problem:

function filter_where($where = '') {
    $where .= " AND post_date < '" . date('Y-m-d') . "'";
    return $where;
}

add_filter('posts_where', 'filter_where');
$query = new WP_Query(array('posts_per_page' => 3));
remove_filter('posts_where', 'filter_where');
0

you are trying to compare the combined date and time custom fields with the current time to filter out posts that are older than the current time.

Replace combined_date_time with the actual meta key name where you store the combined 'date' and 'time'.

Use current_time('timestamp') to get the current time in Unix timestamp format.

Add 'type' => 'NUMERIC' to the meta query to ensure proper numeric comparison

$args = array(
    'post_type'      => 'some_kind_of_posts',
    'posts_per_page' => 3,
    'orderby'        => 'meta_value_num',
    'meta_key'       => 'combined_date_time', // Replace 'combined_date_time' with the actual meta key name
    'order'          => 'DESC',
    'meta_query'     => array(
        array(
            'key'     => 'combined_date_time', // Replace 'combined_date_time' with the actual meta key name
            'value'   => current_time('timestamp'), // Get the current time in Unix timestamp
            'compare' => '<',
            'type'    => 'NUMERIC' // Specify the type as numeric for proper comparison
        ),
    ),
);
$query = new WP_Query($args);

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.