Support

Account

Home Forums General Issues How to order by date field

Solved

How to order by date field

  • I would like to order posts by custom date field, and also I would like to display only posts from one category. Here is the code that I have which does none of those things (instead it shows all the posts from all the catgeories, and they are ordered by published date, not the custom date):

    <?php
     
     
    /*
    *  Order Posts based on Date Picker value
    *  this example expects the value to be saved in the format: yymmdd (JS) = Ymd (PHP)
    */
     
    $posts = get_posts(array(
    	'meta_key' => 'event_date', // name of custom field
    	'orderby' => 'meta_value_num',
    	'order' => 'ASC',
    	'cat' => '23'
    ));?>
    
    	<?php if (query_posts($posts)) : while (have_posts()) : the_post(); ?>
    		
    		
    
    		<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
    
    			<div class="entry">
    				<?php the_post_thumbnail( 'whatson-thumb' ); ?>
    				<h2 class="pagetitle"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
    				<?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
    				<?php the_excerpt(); ?> 
    				
    				<!--<div class="postmetadata">
    				<?php //the_tags('Tags: ', ', ', '<br />'); ?>
    				<span class="category"><?php the_category(', ') ?></span>
    				<?php //comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?>
    				</div>-->
    				
    			</div>
    
    			
    
    		</div>
    
    	<?php endwhile; ?>
    	<?php wp_reset_postdata(); ?>
    
    	<?php //include (TEMPLATEPATH . '/inc/nav.php' ); ?>
    
    	<?php else : ?>
    
    		<h2>Not Found</h2>
    
    	<?php endif; ?>
  • This reply has been marked as private.
  • Will this work with get_posts as well? I have a similar issue what I want to display all the posts with the category “event” and I want to order them based on Advanced custom field called event_date. Here is how I want to display each post:

                <div class="home-post-area">
    
                    <div class="home-post-image">
                    	<?php the_post_thumbnail('', array('class' => 'img-responsive')); ?>
                      <div class="home-post-image-overlay">
                        <h3><?php the_field('event_date'); ?> <?php the_field('event_price'); ?></h3>
                        <h4><?php the_title();?></h4>
                      </div>
                    </div>
    
                    <div class="home-post-content">
                    	<?php the_excerpt();?>
    
                    <p><a href="<?php the_permalink() ?>"><atrong>>> MORE</atrong></a></p>
    
                    </div>
    
                </div>
  • Thank you! The problem must have been with the way I wrote the loop, now it works fine and the category is displayed properly too now.

  • Use the code that Mathias suggested, and place your code after:

    if ( $the_query->have_posts() ) {
    		while ( $the_query->have_posts() ) {
    			
    			$the_query->the_post();

    you can break the php to place your html like this:

    if ( $the_query->have_posts() ) {
    		while ( $the_query->have_posts() ) {
    			
    			$the_query->the_post(); ?>
    
    <!-- your code here -->
    
            <?php } } else { ?>
    
    		<h2>Not Found</h2>
    
    	<?php //endif; ?>
    	<?php } ?>
    	<?php wp_reset_postdata(); ?>
    
    
  • Excellent. It works! So what if I want to only display events with an event_date that is on or after the current date?

  • Ok, tried this:

    <?php 
     
    $today = date('MM d, yy');
     
    $args = array (
        'post_type' => 'post',
        'meta_query' => array(
            array(
                'meta_key'  => 'event_date',
                'compare'   => '>=',
                'value'     => $today,
            ),
             array(
                'meta_key' => 'event_date',
                'orderby' => 'meta_value_num',
                'order' => 'ASC',
                'cat' => '2'
            )
        ),
    );
    
        $the_query = new WP_Query( $args );
        
        
        if ( $the_query->have_posts() ) {
            while ( $the_query->have_posts() ) {
                
                $the_query->the_post(); ?>

    But it does not return the correct result. It shows all the events.

  • Can you please unmark this answer as Private? I’m trying to see your answer to help solve my issue, but for whatever reason you will not let others see it.

  • @zchmm Yea I agree..
    I don’t see how a private answer on a forum post is in any way useful to anyone. I’d say it actually went against the entire point of forum question and answer support.

  • This is a very important topic. I agree with @juliusbangert and @zchmm. This shouldn’t be marked as private. ¬¬

  • This answer marked as private, so this is my contribution:

    
    <?php
    global $wpdb;
    $posts = $wpdb->get_results( '
    	SELECT
    		wp_posts.ID,
    		wp_posts.post_title,
    		wp_posts.post_content
    	FROM wp_posts
    	INNER JOIN wp_postmeta
    		ON wp_posts.ID = wp_postmeta.post_id
    	WHERE
    		wp_postmeta.meta_key = "your_acf_date_var"
    		AND wp_posts.post_type = "your_post_type"
    		AND wp_posts.post_status = "publish"
    	GROUP BY wp_posts.ID
    	ORDER BY wp_postmeta.meta_value ASC
    ' );
    foreach ( $posts as $key => $value ) {
    	var_dump( $value->post_title );
    }
    ?>
    
  • After looking on the internet for the last 4 hours, I finally figure it out. You have to put the orderby metadata first, then add a meta_query array to filter through the posts.

    $args = array (
        'post_type' => 'route',
            'meta_key' => 'trip_start_date',
            'orderby' => 'meta_value_num',
            'order' => 'ASC',
            'meta_query' => array(
                array(
                    'meta_key'  => 'route_status',
                    'compare'   => '=',
                    'value'     => 'not-yet-launched',
                ),
            ),
            'posts_per_page' => '9',
    );
Viewing 13 posts - 1 through 13 (of 13 total)

The topic ‘How to order by date field’ is closed to new replies.