Help Desk

Query events to display free events only

  • Posts: 9 Topics: 4
    | Permalink

    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

    Posts: 209 Topics: 2
    | Permalink

    Hi 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.

    Posts: 9 Topics: 4
    | Permalink

    Thank you Juan. We’ll give it a try

    Posts: 209 Topics: 2
    | Permalink

    My pleasure 🙂

    Have a nice day,

    Juan.

    Posts: 9 Topics: 4
    | Permalink

    Sorry 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?

    Posts: 209 Topics: 2
    | Permalink

    Hi,

    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

    Posts: 9 Topics: 4
    | Permalink

    Thanks 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.

    Posts: 209 Topics: 2
    | Permalink

    Hi,

    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

    Posts: 65
    | Permalink

    Hey 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

    | Permalink

The topic ‘Query events to display free events only’ is closed to new replies.