Home › Forums › Calendar Products › Community Events › Query events to display free events only
- This topic has 7 replies, 2 voices, and was last updated 5 years, 11 months ago by tsfstemalliance.
-
AuthorPosts
-
April 17, 2018 at 11:24 am #1509548tsfstemallianceParticipant
Hello, we are looking for a way to filter events to display “free events” only. I’ve looked into the _EventCost and get_tribe_cost() functions but we couldn’t figure out how use them to filter events by costs
April 18, 2018 at 12:50 pm #1510578juanfraKeymasterHi there,
Thank you for reaching out to us! I hope you’re doing well.
If you mean to add a filter to the filter bar in order to display the free events, that is possible with some custom coding to create custom filters. We have an interesting article that will get you started on this > https://theeventscalendar.com/knowledgebase/creating-custom-filters-for-filter-bar/
If you need to query and display the free events in some template, you can try the following query:
$args = array(
'post_type' => Tribe__Events__Main::POSTTYPE,
'meta_query' => array(
array(
'key' => '_EventCost',
'value' => '0',
'compare' => '=',
)
),
'posts_per_page' => -1,
);$free_events = new WP_Query( $args );
I hope that helps! Let me know if you have other questions
Best,
Juan.April 18, 2018 at 1:57 pm #1510644tsfstemallianceParticipantThank you Juan. We’ll give it a try
April 18, 2018 at 1:59 pm #1510646juanfraKeymasterMy pleasure 🙂
Have a nice day,
Juan.
April 18, 2018 at 2:35 pm #1510695tsfstemallianceParticipantSorry again Juan I have a followup question. I created a custom template, created a blank page and assigned the custom template for the page. However, the page came out blank on the front end. I also went further to put the template inside the community folder in accordance with your templating instructions but still came up blank. I used the WP Debug to display errors but none, just the white screen.
The questions is: How would I use the code in a custom template to display events on the front end?
April 18, 2018 at 2:48 pm #1510706juanfraKeymasterHi,
Thank you for the follow-up.
Unfortunately, custom development tasks are outside of our stated scope of support. The code I’ve shared with you only queries the database but doesn’t parse the results.
To parse the results, after you make the query you’ll need to go through the loop:
if ( $free_events->have_posts() ) { // The Loop while ( $free_events->have_posts() ) { $free_events->the_post(); echo get_the_title(); } wp_reset_postdata(); }
I’d recommend you to take a look at the WordPress documentation regarding WP_Query.
I hope this helps!
Regards,
Juan
April 18, 2018 at 2:59 pm #1510711tsfstemallianceParticipantThanks Juan for your quick reply. I just realized also that I need to create a function for it as noted here:
function display_free_events(){ if (is_page('Free Events')){ $args = array( 'post_type' => Tribe__Events__Main::POSTTYPE, 'meta_query' => array( array( 'key' => '_EventCost', 'value' => '0', 'compare' => '=', ) ), 'posts_per_page' => -1, ); $free_events = new WP_Query( $args ); } } add_action('genesis_loop', 'display_free_events');
I’ll integrate the code you just sent.
April 19, 2018 at 5:47 am #1511082juanfraKeymasterHi,
Thank you for the follow-up.
Yes, regarding how genesis work and how to handle templates, I guess the best place to look for assistance would be their help desk. But that code should make it.
I hope this helps! If you have any other questions please feel free to let me know and I’d be happy to help as best I can!
Regards,
Juan
May 11, 2018 at 9:35 am #1528328Support DroidKeymasterHey there! This thread has been pretty quiet for the last three weeks, so we’re going to go ahead and close it to avoid confusion with other topics. If you’re still looking for help with this, please do open a new thread, reference this one and we’d be more than happy to continue the conversation over there.
Thanks so much!
The Events Calendar Support Team -
AuthorPosts
- The topic ‘Query events to display free events only’ is closed to new replies.