Query events to display free events only

Home Forums Calendar Products Community Events Query events to display free events only

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #1509548
    tsfstemalliance
    Participant

    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

    #1510578
    juanfra
    Keymaster

    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.

    #1510644
    tsfstemalliance
    Participant

    Thank you Juan. We’ll give it a try

    #1510646
    juanfra
    Keymaster

    My pleasure 🙂

    Have a nice day,

    Juan.

    #1510695
    tsfstemalliance
    Participant

    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?

    #1510706
    juanfra
    Keymaster

    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

    #1510711
    tsfstemalliance
    Participant

    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.

    #1511082
    juanfra
    Keymaster

    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

    #1528328
    Support Droid
    Keymaster

    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

Viewing 9 posts - 1 through 9 (of 9 total)
  • The topic ‘Query events to display free events only’ is closed to new replies.