0

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?

1 Answer 1

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.

1
  • but that is searching a post date, not a custom field with a date
    – rob.m
    Nov 14, 2018 at 0:54

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.