CPT Event Queries

Custom Loops For Events

WP QUERY @ WORDPRESS HANDBOOK

Get all events ordered by 'Start Date' custom field

$event_query = new WP_Query(array(
    'post_type'         => 'event',
    'posts_per_page'    => -1,
    'meta_key'          => 'start_date',
    'orderby'           => 'meta_value',
    'order'             => 'ASC',
)); 

Get all events ordered by 'Start Date' custom field where 'Start Date' is in the future

$event_past_query = new WP_Query(array(
    'post_type'         => 'event',
    'posts_per_page'    => -1,
    'meta_key'          => 'start_date',
    'orderby'           => 'meta_value',
    'order'             => 'ASC',
    'meta_value'   => date( "Ymd" ), 
    'meta_compare' => '>',
));

Get all events ordered by 'Start Date' custom field where 'Start Date' is in the past

$event_past_query = new WP_Query(array(
    'post_type'         => 'event',
    'posts_per_page'    => -1,
    'meta_key'          => 'start_date',
    'orderby'           => 'meta_value',
    'order'             => 'ASC',
    'meta_value'   => date( "Ymd" ), 
    'meta_compare' => '<',
));

Get all events with custom taxonomy 'event-category' , where 'event-category' id = 8

$event_featured_query = new WP_Query(array(
    'post_type'         => 'event',
    'posts_per_page'    => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'event-category',   // taxonomy name
            'field' => 'term_id',             // term_id, slug or name
            'terms' =>  8,                    // term id, term slug or term name
        )
    )
));

Get all events published before today

$today = getdate();
$event_past_query = new WP_Query(array(
    'post_type'         => 'event',
    'posts_per_page'    => -1,
    'meta_key'          => 'start_date',
    'orderby'           => 'meta_value',
    'order'             => 'ASC',
    'date_query'        =>  array(
         array(
                  'before'     => array(
            'year'  =>  $today['year'],
            'month' =>  $today['mon'],
            'day'   =>  $today['mday'],
           ),
        ),
     )
));

Get all events published today

$today = getdate();
$event_past_query = new WP_Query(array(
    'post_type'         => 'event',
    'posts_per_page'    => -1,
    'meta_key'          => 'start_date',
    'orderby'           => 'meta_value',
    'order'             => 'ASC',
    'date_query'        =>  array(
         array(
                  'before'     => array(
            'year'  =>  $today['year'],
            'month' =>  $today['mon'],
            'day'   =>  $today['mday'],
           ),
        ),
     )
));

Get all events published between 2 dates

$today = getdate();
$event_past_query = new WP_Query(array(
    'post_type'         => 'event',
    'posts_per_page'    => -1,
    'meta_key'          => 'start_date',
    'orderby'           => 'meta_value',
    'order'             => 'ASC',
    'date_query'        =>  array(
          array(
            'after'     => 'January 1st, 2013',
            'before'    => array(
              'year'  => 2013,
              'month' => 2,
              'day'   => 28,
            ),
          'inclusive' => true,
        ),
     )
));
Home

- Build with Gatsby -

Blog