Apply display event date and ordering and filter in Event Archive page, Change URl based query Wordpress

 First go to archive-event.php page and find list of event code.

From copy dynamic display code from front-page.php and past into it.

 <span class="event-summary__month"><?php  
          $eventDate = new DateTime(get_field('event_date'));
          echo $eventDate->format('M');
          ?></span>
          <span class="event-summary__day"><?php echo $eventDate->format('d');  ?>
          </span>

and save


after that we will order and filter event posts according to event date and hide posts which event passed.

So here we can not use custom query because wordpress have default query working here to display events so we will add action and function in function.php file .

So write below code in functions .php

function university_adjust_queries($query) {
  if(!is_admin() && is_post_type_archive('event') && $query->is_main_query()) {
    //  $query->set('posts_per_page', 1); for testing purposes
    $today = date('Ymd');
    $query->set('meta_key', 'event_date');
    $query->set('orderby', 'meta_value_num');
    $query->set('order', 'ASC');
    $query->set('meta_query', array( array(
                  'key' => 'event_date',
                  'compare' => '>=',
                  'value' => $today,
                  'type' => 'numeric'
                ) ));
  }
   
}

add_action('pre_get_posts', 'university_adjust_queries');

and save


and in front-page.php also for event display number of posts change -1 to 2



Comments

Popular posts from this blog

Basics Of PHP

Creating App.js for Node Application

WordPress Full Guide For Developer