$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - How to compare between two dates in a nested loop?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far