Support

Account

Home Forums Front-end Issues query by date

Solved

query by date

  • Hi
    I just started to use this grate plugin and i wanted to query some event custom posts with the date custom field.
    I created a custom field, called “event_datum”.
    The date format is yymmdd.(preset from acf)
    I used the code bellow to filter out the events that are gona be in the future.
    Unfortunately id does not filter it out.
    After some research i’m quite shure it has to be an issue of the date format.
    Hope someone has an answer on that topic.
    Thanks

    <?php $events = new WP_Query(
    array(
    ‘post_not_in’ => array($donotrepeat),
    ‘post_type’ => ‘chuchi_event’,
    ‘meta_key’ => ‘event_datum’,
    ‘orderby’ => ‘meta_value’,
    ‘order’ => ‘dsc’,
    “posts_per_page” => 10,
    ‘meta_query’ => array(
    ‘key’ => ‘event_datum’,
    ‘value’ => date(“yymmdd”),
    ‘compare’ => ‘<=’,
    ‘type’ => ‘DATE’
    )

    )); ?>

    <?php while($events->have_posts()) : $events->the_post(); ?>

  • Hi @retotito

    Thanks for the question.

    I can see that your code is using the following as the meta value:

    
    ‘value’ => date(“yymmdd”),
    

    Please note that the date field “yymmdd” is a javascript format string which differs from a PHP format string.

    You can read more about this on the date field docs page, but basically all you need to do is change it to:

    
    ‘value’ => date(“Ymd”),
    

    Thanks
    E

  • Hi Elliot
    Thanks for your answer.
    I did try dad, but somehow it still does not filter it.
    Maybe there is something else wrong with the code.

  • Hi @retotito

    Can you please debug your WP_Query $args?
    What do they look like?

  • Hi elliot.
    when i use

    <?php   
         var_dump($args);   
           ?> 
    
    it gives me following information:
    
    

    object(WP_Query)#2729 (47) { [“query_vars”]=> array(64) { [“post_not_in”]=> array(1) { [0]=> NULL } [“post_type”]=> string(12) “chuchi_event” [“meta_key”]=> string(11) “event_datum” [“orderby”]=> string(10) “meta_value” [“order”]=> string(4) “DESC” [“meta_query”]=> array(4) { [“key”]=> string(11) “event_datum” [“value”]=> string(8) “20131217” [“compare”]=> string(2) “<=” [“type”]=> string(4) “DATE” } [“error”]=> string(0) “” [“m”]=> string(0) “” [“p”]=> int(0) [“post_parent”]=> string(0) “” [“subpost”]=> string(0) “” [“subpost_id”]=> string(0) “” [“attachment”]=> string(0) “” [“attachment_id”]=> int(0) [“name”]=> string(0) “” [“static”]=> string(0) “” [“pagename”]=> string(0) “” [“page_id”]=> int(0) [“second”]=> string(0) “” [“minute”]=> string(0) “” [“hour”]=> string(0) “” [“day”]=> int(0) [“monthnum”]=> int(0) [“year”]=> int(0) [“w”]=> int(0) [“category_name”]=> string(0) “” [“tag”]=> string(0) “” [“cat”]=> string(0) “” [“tag_id”]=> string(0) “” [“author”]=> string(0) “” [“author_name”]=> string(0) “” [“feed”]=> string(0) “” [“tb”]=> string(0) “” [“paged”]=> int(0) [“comments_popup”]=> string(0) “” [“meta_value”]=> string(0) “” [“preview”]=> string(0) “” [“s”]=> string(0) “” [“sentence”]=> string(0) “” [“fields”]=> string(0) “” [“menu_order”]=> string(0) “” [“category__in”]=> array(0) { } [“category__not_in”]=> array(0) { } [“category__and”]=> array(0) { } [“post__in”]=> array(0) { } [“post__not_in”]=> array(0) { } [“tag__in”]=> array(0) { } [“tag__not_in”]=> array(0) { } [“tag__and”]=> array(0) { } [“tag_slug__in”]=> array(0) { } [“tag_slug__and”]=> array(0) { } [“post_parent__in”]=> array(0) { } [“post_parent__not_in”]=> array(0) { } [“author__in”]=> array(0) { } [“author__not_in”]=> array(0) { } [“ignore_sticky_posts”]=> bool(false) [“suppress_filters”]=> bool(false) [“cache_results”]=> bool(true) [“update_post_term_cache”]=> bool(true) [“update_post_meta_cache”]=> bool(true) [“posts_per_page”]=> int(10) [“nopaging”]=> bool(false) [“comments_per_page”]=> string(2) “50” [“no_found_rows”]=> bool(false) } [“tax_query”]=> object(WP_Tax_Query)#2753 (2) { [“queries”]=> array(0) { } [“relation”]=> string(3) “AND” } [“meta_query”]=> object(WP_Meta_Query)#2727 (2) { [“queries”]=> array(1) { [0]=> array(1) { [“key”]=> string(11) “event_datum” } } [“relation”]=> string(3) “AND” } [“date_query”]=> bool(false) [“post_count”]=> int(4) [“current_post”]=> int(-1) [“in_the_loop”]=> bool(false) [“comment_count”]=> int(0) [“current_comment”]=> int(-1) [“found_posts”]=> string(1) “4” [“max_num_pages”]=> float(1) [“max_num_comment_pages”]=> int(0) [“is_single”]=> bool(false) [“is_preview”]=> bool(false) [“is_page”]=> bool(false) [“is_archive”]=> bool(false) [“is_date”]=> bool(false) [“is_year”]=> bool(false) [“is_month”]=> bool(false) [“is_day”]=> bool(false) [“is_time”]=> bool(false) [“is_author”]=> bool(false) [“is_category”]=> bool(false) [“is_tag”]=> bool(false) [“is_tax”]=> bool(false) [“is_search”]=> bool(false) [“is_feed”]=> bool(false) [“is_comment_feed”]=> bool(false) [“is_trackback”]=> bool(false) [“is_home”]=> bool(true) [“is_404”]=> bool(false) [“is_comments_popup”]=> bool(false) [“is_paged”]=> bool(false) [“is_admin”]=> bool(false) [“is_attachment”]=> bool(false) [“is_singular”]=> bool(false) [“is_robots”]=> bool(false) [“is_posts_page”]=> bool(false) [“is_post_type_archive”]=> bool(false) [“query_vars_hash”]=> string(32) “77f06eaf00a501b6f2c861adfd3a0c7a” [“query_vars_changed”]=> bool(false) [“thumbnails_cached”]=> bool(false) [“stopwords”:”WP_Query”:private]=> NULL [“query”]=> array(6) { [“post_not_in”]=> array(1) { [0]=> NULL } [“post_type”]=> string(12) “chuchi_event” [“meta_key”]=> string(11) “event_datum” [“orderby”]=> string(10) “meta_value” [“order”]=> string(3) “dsc” [“meta_query”]=> array(4) { [“key”]=> string(11) “event_datum” [“value”]=> string(8) “20131217” [“compare”]=> string(2) “<=” [“type”]=> string(4) “DATE” } } [“request”]=> string(355) “SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1 AND wp_posts.post_type = ‘chuchi_event’ AND (wp_posts.post_status = ‘publish’ OR wp_posts.post_status = ‘closed’) AND (wp_postmeta.meta_key = ‘event_datum’ ) GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value DESC LIMIT 0, 10” [“posts”]=> array(4) { [0]=> object(WP_Post)#2763 (24) { [“ID”]=> int(137) [“post_author”]=> string(1) “1” [“post_date”]=> string(19) “2013-12-14 03:10:48” [“post_date_gmt”]=> string(19) “2013-12-14 03:10:48” [“post_content”]=> string(1781) “Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonu ……..

  • Hi @retotito

    Instead of debugging the WP_Query object, can you please debug just the $args like so:

    <?php 
    
    $args = array(
    	'post_not_in' => array($donotrepeat),
    	'post_type' => 'chuchi_event',
    	'meta_key' => 'event_datum',
    	'orderby' => 'meta_value',
    	'order' => 'dsc',
    	'posts_per_page' => 10,
    	'meta_query' => array(
    		'key' => 'event_datum',
    		'value' => date('yymmdd'),
    		'compare' => '<=',
    		'type' => 'DATE'
    	)
    );
    
    echo '<pre>';
    	print_r( $args );
    echo '</pre>';
    die;
    
    $events = new WP_Query($args);
    
    	
    ?>

    P.S. Please wrap all code in the code buttons
    Thanks
    E

  • Hi Elliot

    That’s the code i used:

    <?php $args = 
         			array(
         				'post_not_in'    => array($donotrepeat),
                                    'post_type'       => 'chuchi_event',
         				'meta_key'        => 'event_datum',
         				'orderby'        => 'meta_value',
         				'order'           => 'dsc',
                'meta_query'  => array(         
                      'key'     => 'event_datum',  
                      'value'   => date("Ymd"),  
                      'compare' => '<=',          
                      'type'    => 'DATE'         
                                      )     					           
         			      ); 
            
              echo '<pre>';
    	        print_r( $args );
              echo '</pre>';
              die;
         
            $events = new WP_Query($args);
         
            ?>

    That’s the output:

    Array
    (
        [post_not_in] => Array
            (
                [0] => 
            )
    
        [post_type] => chuchi_event
        [meta_key] => event_datum
        [orderby] => meta_value
        [order] => dsc
        [meta_query] => Array
            (
                [key] => event_datum
                [value] => 20131218
                [compare] => <=
                [type] => DATE
            )
    
    )

    I hope it’s what you are looking for.
    Thanks

  • Hi @retotito

    The issue could be caused by the ‘post_not_in’ argument. Can you remove that as it is not doing anything at the moment.

    Also, can you remove the type => ‘DATE’ as this is not neccessary.

    Can you then confirm that your date field has a save_format setting of ‘yymmdd’? Please also update a few of your posts to ensure that the vaue is saved in the correct format.

    Thanks
    E

  • Hi Elliot.
    Thanks for your help.

    I Tryed all of that:
    removed the ‘post_not_in’ argument.
    removed the type => ‘DATE’
    the save_format setting is ‘yymmdd’
    i updated the post and added some new one’s
    i deactivated plugins

    but unfortunately it still doesn’t filter.

    By the way the order by date works fine.

    I might try to redo the same situation with another wp theme, maybe thats where the issue commes from.

  • Hi @retotito

    It is possible that there is a filter in your theme or a plugin which is modifying the WP_Query args. This filter is most likely called ‘pre_get_posts’.

    Perhaps as a test, create a simple text field and enter the value ‘test’. Then change your query args to find posts where the field_name = ‘test’.

    Does this work?

    Thanks
    E

  • Hi Elliot

    That works fine.
    I tested with following code:

    <?php $args = new WP_Query(
         			array(
                'post_type' => 'testpost',
                'meta_key' => 'testpost_testtext', 
    	    'meta_value' => 'test', 
              
              ));     
         			?>

    Do you suggest to ask the theme-support about that issue?
    I’m using the “enfold” theme.

  • Hi @retotito

    Personally, I would search through your theme’s code before contacting the developers. But sure, they will be able to help.

    Start with the functions.php file.

    Thanks
    E

  • Hi Elliot
    Thanks first for the outstanding support.
    I’m going to check that out soon and let you know.

    For the meantime i got a workaround with an if statement. That might not be the most beautiful solution, but it works temporarely:

    `<?php $args = new WP_Query(
    array(
    ‘post_type’ => ‘chuchi_event’,
    ‘meta_key’ => ‘event_datum’,
    ‘orderby’ => ‘meta_value’,
    ‘order’ => ‘dsc’,
    ‘posts_per_page’ => 50,

    )); ?>

    <?php while($args->have_posts()) : $args->the_post(); ?>

    <?php
    $datex = strtotime(date_format(date_create(”.get_field(‘event_datum’).”),’d-m-Y’));
    $now = time();
    ?>

    <?php
    if($now <= ($datex+(60*60*24))){
    ?>
    <div>
    </div>
    <?php
    }
    ?>

    <?php endwhile; ?>

Viewing 13 posts - 1 through 13 (of 13 total)

The topic ‘query by date’ is closed to new replies.