Last active
January 5, 2023 17:24
-
-
Save jo-snips/5112025 to your computer and use it in GitHub Desktop.
The Events Calendar - Custom Query Using WP_Query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$args = array( | |
'post_status'=>'publish', | |
'post_type'=>array(TribeEvents::POSTTYPE), | |
'posts_per_page'=>10, | |
//order by startdate from newest to oldest | |
'meta_key'=>'_EventStartDate', | |
'orderby'=>'_EventStartDate', | |
'order'=>'DESC', | |
//required in 3.x | |
'eventDisplay'=>'custom', | |
//query events by category | |
'tax_query' => array( | |
array( | |
'taxonomy' => 'tribe_events_cat', | |
'field' => 'slug', | |
'terms' => 'featured', | |
'operator' => 'IN' | |
), | |
) | |
); | |
$get_posts = null; | |
$get_posts = new WP_Query(); | |
$get_posts->query($args); | |
if($get_posts->have_posts()) : while($get_posts->have_posts()) : $get_posts->the_post(); ?> | |
<a href="<?php the_permalink(); ?>"> | |
<?php the_title(); ?> | |
</a><br /> | |
<?php if (tribe_get_start_date() !== tribe_get_end_date() ) { ?> | |
<?php echo tribe_get_start_date(); ?> - <?php echo tribe_get_end_date(); ?> | |
<?php } else { ?> | |
<?php echo tribe_get_start_date(); ?> | |
<?php } ?> | |
<?php the_content(); ?> | |
<?php | |
endwhile; | |
endif; | |
wp_reset_query(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It looks like 'TribeEvents' in line 5 is deprecated and we should be using 'Tribe__Events__Main'. I just ran into this issue and have had to update all of my custom events queries. This is a change since version 3.10 and when I made my change, it caused me date formatting issues with tribe_get_start_date(). Just thought I would share. Also, to get the correct date format, I followed Airton's example and used:
$month = tribe_get_start_date ( $post->ID, false, 'M');
$day = tribe_get_start_date ( $post->ID, false, 'j');