wp query - How to compare between two dates in a nested loop?

admin2025-06-06  10

I have a custom field like this:

$myDate = usp_get_meta(false, 'usp-custom-51');

That gives me dates like: 22/2/2011

Then I create a loop which first checks if a term $s via GET is in any category and if so display its post

while ( have_posts() ) : the_post();
 if($sel === "tema") {
   $terms = get_terms( 'category', array(
   'name__like' => $s,
   'category__not_in' =>  183,
   'include_children' => false,
   'hide_empty' => true,
   'post_type'  => 'post'
) );
if ( count($terms) > 0 ){
?>
    <li>Lorem...</li>

And that works, and I can see the posts. But now I need to do a double check in regards of that found post is between certain dates:

So I have:

$start = $_GET['start']; 
$end = $_GET['end']; 

Which gives me:

22/2/2011 and 15/7/2015

And now I am trying to create a new query which checks if the post is within these dates:

$newId = get_cat_ID();
$args = array(
'cat' => $cat_ID,
'meta_query' => array(
    'relation' => 'AND',
    array(
        'key'     => $myDate,
        'value'   => $start,
        'compare' => '>=',
        'type'    => 'DATE'
    ),
    array(
        'key'     => $myDate,
        'value'   => $end,
        'compare' => '<=',
        'type'    => 'DATE'
    )
),
'orderby' => 'date',
'order' => 'DESC'
);
$events_query = new WP_query();
$events_query->query($args);
if( $events_query->have_posts() ) : while( $events_query->have_posts() ) : $events_query->the_post(); ?>

<li>Lorem...</li>

But that gives me zero results.

I tried:

'meta_query' => array(
   array(
     'key' => $myDate,
     'value' => array($start, $end),
     'compare' => 'BETWEEN',
     'type' => 'DATE'
   ),
)

And I tried to use one single query like:

while ( have_posts() ) : the_post();
    if($sel === "tema") {
        $terms = get_terms( 'category', array(
            'name__like' => $s,
            'category__not_in' =>  183,
            'include_children' => false,
            'hide_empty' => true,
            'post_type'  => 'post',
            'meta_query' => array (
               array(
                 'key' => $myDate,
                 'value' => array($start, $end),
                 'compare' => 'BETWEEN',
                 'type' => 'DATE'
               ),
            )
        ) );
        if ( count($terms) > 0 ){ 
        ?>
            <li>Lorem...</li>

But still, zero results.

Any idea?

I have a custom field like this:

$myDate = usp_get_meta(false, 'usp-custom-51');

That gives me dates like: 22/2/2011

Then I create a loop which first checks if a term $s via GET is in any category and if so display its post

while ( have_posts() ) : the_post();
 if($sel === "tema") {
   $terms = get_terms( 'category', array(
   'name__like' => $s,
   'category__not_in' =>  183,
   'include_children' => false,
   'hide_empty' => true,
   'post_type'  => 'post'
) );
if ( count($terms) > 0 ){
?>
    <li>Lorem...</li>

And that works, and I can see the posts. But now I need to do a double check in regards of that found post is between certain dates:

So I have:

$start = $_GET['start']; 
$end = $_GET['end']; 

Which gives me:

22/2/2011 and 15/7/2015

And now I am trying to create a new query which checks if the post is within these dates:

$newId = get_cat_ID();
$args = array(
'cat' => $cat_ID,
'meta_query' => array(
    'relation' => 'AND',
    array(
        'key'     => $myDate,
        'value'   => $start,
        'compare' => '>=',
        'type'    => 'DATE'
    ),
    array(
        'key'     => $myDate,
        'value'   => $end,
        'compare' => '<=',
        'type'    => 'DATE'
    )
),
'orderby' => 'date',
'order' => 'DESC'
);
$events_query = new WP_query();
$events_query->query($args);
if( $events_query->have_posts() ) : while( $events_query->have_posts() ) : $events_query->the_post(); ?>

<li>Lorem...</li>

But that gives me zero results.

I tried:

'meta_query' => array(
   array(
     'key' => $myDate,
     'value' => array($start, $end),
     'compare' => 'BETWEEN',
     'type' => 'DATE'
   ),
)

And I tried to use one single query like:

while ( have_posts() ) : the_post();
    if($sel === "tema") {
        $terms = get_terms( 'category', array(
            'name__like' => $s,
            'category__not_in' =>  183,
            'include_children' => false,
            'hide_empty' => true,
            'post_type'  => 'post',
            'meta_query' => array (
               array(
                 'key' => $myDate,
                 'value' => array($start, $end),
                 'compare' => 'BETWEEN',
                 'type' => 'DATE'
               ),
            )
        ) );
        if ( count($terms) > 0 ){ 
        ?>
            <li>Lorem...</li>

But still, zero results.

Any idea?

Share Improve this question edited Nov 13, 2018 at 23:49 rob.m asked Nov 13, 2018 at 23:28 rob.mrob.m 2072 silver badges9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

That date format will be a problem for wp_query because it's a string in the format d-m-y.

Your meta_query is close but the key needs to be the field name, and the stored date format must be yyyy-mm-dd:

'meta_query' => array(
   array(
     'key' => 'usp-custom-51',
     'value' => array($start, $end),
     'compare' => 'BETWEEN',
     'type' => 'DATE'
   ),
)

If you can't change your stored date format, it might be easiest to get all posts in the category and do your date filtering with PHP.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749183962a317063.html

最新回复(0)