0

Hello all i am struggling for days now, and i hope one of you can help me i have a custom post type called events and i like to show only events that date is today and upcoming but i couldnt get it to work , what am i doing wrong ?

here is my code so far:

<?php
$paged = ( get_query_var('paged') ) ?get_query_var('paged') : 1;
$today = time();
$events = new WP_Query(array(
    'post_type' => 'events',
    'posts_per_page' => '15',
    'paged' => $paged,        
    'meta_query' => array(
         array(
              'key' => 'closingdate',
              'compare' => '>=',
              'value' => $today,                             
           )
      ),
    'meta_key' => 'closingdate',
    'orderby' => 'meta_value',
    'order' => 'DESC',
));
?> 

                   <?php if ($events->have_posts()) :  while ($events->have_posts()) : $events->the_post();?>
            <div class="row">
                    <div class="cell_left"><p><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></p></div>
                    <div class="cell"><p class="posted"><?php echo date('d-m-Y', strtotime(get_post_meta($post->ID, "closingdate",true)));?></p></div>                                            
                  </div>
                   <?php endwhile;?>  
            </div>                
            <div class="navigation">      
            <?php wp_pagenavi( array( 'query' => $events ) ); ?>           
          <?php wp_reset_query();?> 
0

Your main issue is here is date formatting. Although the other answer will work, it is ineffecient and will cause inconsistant amount of posts per page in paginated queries. The issue here is, if you have 100 posts and only 10 has a date in future, you are wasting a lot of resources querying an extra 90 posts to just skip/hide them in the loop. Also, if you paginate the query, you might get pages with 5 or two or no posts at all at random. But I'm not here to crirtisize any other posts ;-)

As I said, your main issue is date formatting. Unlike the date_query field, custom fields sorts and compare literally. What that means is, if you try to compare an apple with an orange as a fruit, the comparison will fail in a custom field as an orage and an apple is not the same thing, although they are both fruits, where in a date_query the comparison will pass as the logic is there that both are fruits regardless to which kind of fruit

time() returns the current time in unix timestamp. You can test this by simply doing echo time(); anywhere in a template. In a date_query, the logic is there which knows this is a valid unix timestamp. A meta_query sees this a literal number with no special meaning. In order to make sorting and comparison work, your date in a custom field must also be saved as unix timestamp in order for this format to work. Any other format will fail.

I believe that the dates in your custom field is saved as normal dates and not in unix timestamp, so the above will not work with time(). What you need is the date() function or similar functions in php to return the current time and date in the required format (Note, your date format saved in a custom field have to be Y-m-d H:i:s for comparison or sorting purposes, any other format will not work, period).

So, to solve your issue, if your date format is Y-m-d H:i:s or just Y-m-d, you only need to change the following line

$today = time();

to

$today = date( 'Y-m-d H:i:s' );

or

$today = date( 'Y-m-d' );

Apart from this, your code should work and only return posts after and including today

PS! wp_reset_query(); should be wp_reset_postdata(); ;-)

  • My pleasure, glad my answer helped :-). Enjoy – Pieter Goosen Jul 28 '15 at 15:04
0

Jeroen your Quection is Custom post type Event display but old Event not display so you can use this code Please insert your meta value date and and current date to strtotime to check date is today or not condition.

<?php 
$YOUR_CURRENT_DATE = date('d-M-Y');
$args = array( 'post_type' => 'event', 'posts_per_page' => -1,'orderby' => 'menu_order ID','order'   => 'ASC' );
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
$YOUR_METADATE = get_post_meta( $post->ID, 'YOUR_META_DATE', true );
if(strtotime($YOUR_CURRENT_DATE) <= strtotime($YOUR_METADATE)  ):
?>

        <!--  Your code here -->
        <!--  Old Event is hide -->

<?php 
endif;
endwhile;       
wp_reset_postdata();
?>
  • This is ineffecient and will have issues with inconsistent amount of posts per page – Pieter Goosen Jul 26 '15 at 17:20
  • @Pieter , do you have a better solutions for this ? – Jeroen Jul 26 '15 at 18:07
  • Yes, your date format for today and the date format saved in custom field must match, otherwise your comparison and sorting will fail. Also, your date format in a custom field must be unix timestamp or in the format of Y-m-d. Any other format will fail. The OP has one or both of these issues, that is w7hy his query is not working ;-) – Pieter Goosen Jul 26 '15 at 18:32
  • Thanx, Pieter Goosen – Mahavar Hitesh Jul 27 '15 at 5:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.