Support

Account

Home Forums ACF PRO Order CPT by Date Picker then by Time Picker

Solving

Order CPT by Date Picker then by Time Picker

  • I have an agenda list that groups by day and then lists all events during that day. In each group I need it to order the posts by time picker.

    This is what I have:

    
    <?php $posts = get_posts(array(
        'post_type' => 'sessions',
        'meta_key'  => 'session_date',
        'orderby'   => 'meta_value_num',
        'order'     => 'ASC',
        'posts_per_page' => -1
    ));
    
    $group_posts = array();
    
    if( $posts ) :
    
        foreach( $posts as $post ) :
    
            $date = get_field('session_date', $post->ID, false);
            $date = new DateTime($date);
            $day = $date->format('F j, Y');
            $group_posts[$day][] = array($post, $date);
    
            endforeach;
    
    endif; ?>
    
    <div class="card-box agenda-tabs accordion-spacing">
        <div class="sd-session-tabs sd-session-event">
            <ul class="tabs" data-tabs id="session-tabs">
                <?php
                $content = 0; 
                foreach ($group_posts as $dayKey => $days) : ?>
                    <li><a class="tab-link session-<?php echo $content; ?>-content" data-tab="session-<?php echo $content; ?>-content"><?php echo $dayKey; ?></a></li>
                    <?php $content++ ?>
                <?php endforeach; ?>
            </ul>
    
            <div class="tabs-content" data-tabs-content="session-tabs">
                <?php $content  = 0; ?>
                <?php foreach ($group_posts as $dayKey => $days) : 
    
                    $date_query = new WP_Query(array(
                        'post_type'         =>'sessions', 
                        'posts_per_page'    => -1,
                        'order'             => 'ASC',
                        'meta_query'        => array(
                            'relation'  => 'AND',
                            'date_q' => array(
                                'key'       => 'session_date', 
                                'compare'   => '=',
                                'value'     => $dayKey,
                                'type'      => 'date'
                            ), 
                            'time_q' => array(
                                'key'       => 'start_time', 
                                'compare'   => 'EXISTS',
                                'type'      => 'TIME'
                            ), 
                        ),
                        'orderby'        => 'meta_key',
                        'order'          => 'ASC'
                    ));  ?>
    
                    <div class="tab-content session-<?php echo $content; ?>-content" id="session-<?php echo $content; ?>-content">
                        
                        <?php if ( $date_query->have_posts() ) : ?>
                            <div class="sd-widget-content sd-session-event">
                                <div class="sd-widget-details-box">
                                    <ul>
                                        <?php while ( $date_query->have_posts() ) : $date_query->the_post(); ?>
    
                                            <li><!-- How the events look and how they are displayed --></li>
                                            
                                        <?php endwhile; ?>
                                    </ul>
                                </div>
                            </div>
                            <?php wp_reset_postdata();
    
                        else : ?>
                            <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
                        <?php endif; ?>
    
                    </div>
        
                    <?php $content++ ?>
                <?php endforeach; ?> 
            </div>
        </div>
    </div>  
    
    
  • To to this you need to use a meta_query instead of a simple meta_key. See https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters, look at the 10th code example under ‘orderby’ with multiple ‘meta_key’s

  • That just unordered my initial dates and still does not order the posts by time.
    It is supposed to show up like:
    Day 1
    Event 1 – 7am
    Event 2 – 8am
    Event 3 – 9am
    Day 2
    Event 4 – 7am
    Event 5 – 8am
    Event 6 – 9am
    Day 3
    Event 7 – 7am
    Event 8 – 8am
    Event 9 – 9am

  • My updated 2nd query and it still does not order the posts right. With this second query I need the posts to be ordered by time. The meta_query shows posts that match the date and then it should order them by start_time

    
    $date_query = new WP_Query(array(
        'post_type'         =>'sessions', 
        'posts_per_page'    => -1,
        'meta_query' => array(
            'date_clause' => array(
                'key'       => 'session_date', 
                'compare'   => '=',
                'value'     => $dayKey,
                'type'      => 'date'
            ),
            'time_clause' => array(
                'key' => 'start_time',
                'compare' => 'EXISTS',
            ), 
        ),
        'orderby' => array( 
            'time_clause' => 'ASC',
        ),
    ));  ?>
    
  • 
    $date_query = new WP_Query(array(
                        'post_type'         =>'sessions', 
                        'posts_per_page'    => -1,
                        'meta_query'        => array(
                            'relation'  => 'AND',
                            'date_q' => array(
                                'key'       => 'session_date', 
                                'compare'   => '=',
                                'value'     => $dayKey,
                                'type'      => 'date'
                            ), 
                            'time_q' => array(
                                'key'       => 'start_time', 
                                'compare'   => 'EXISTS',
                                'type'      => 'TIME'
                            ), 
                        ),
                        'orderby'        => array('date_q' => 'ASC', 'time_q' => 'ASC')
                    ));  ?>
    
    
  • I have added your code and it still does not work. Here is a link to my site and the page it is implemented on – https://www.cosmeticsurgeryforum.com/agenda/

  • You’ve got 2 queries in the code and I don’t see the need for the second. Altering the first query in the way I mentioned will order the post by the two fields, there wouldn’t be any need to do additional queries on every date.

    Also, this is incorrect

    
    
                'type'      => 'date'
    

    ACF does not store date fields in date format, it stores them like YYYYMMDD so this query will find nothing that matches the date.

  • The first query gets the dates picked in the posts and the second query compares all posts to the dates and lists all of the post that have picked that date, for example:
    July 10th, 2019

    • Post Name 1 – start time 7am
    • Post Name 2 – start time 8am
    • Post Name 3 – start time 9am

    July 11th, 2019

    • Post Name 4 – start time 6am
    • Post Name 5 – start time 9am
    • Post Name 6 – start time 11am

    That is what I need the code to do, but it is currently ordering it by published date/time.

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

The topic ‘Order CPT by Date Picker then by Time Picker’ is closed to new replies.