How to remove past events from list

Home Forums Calendar Products Events Calendar PRO How to remove past events from list

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #1155648
    Christian Thomson
    Participant

    Hello TEC!

    On the website we built for our client, Valley First Aid Training, we created a custom loop to show the Courses in a list by category.

    Example: https://valleyfirstaidtraining.ca/valley-first-aid-courses/worksafebc-first-aid/occupational-first-aid-level-2/

    However, there are past events which are showing first in the list, and customers are registering (by accident) for events that have already happened.

    Can you help us figure out how not to show the past events in the courses lists?

    Thanks!

    C

    PS: Can you reply cc to [email protected]?

    #1155810
    Geoff
    Member

    Hey there, C!

    Great question. Do you have an example of how the custom loop works? I do know that it is possible to exclude past events if you’re using the tribe_get_events function to create the loop.

    In fact, here is a great tutorial on how to build a loop of events using the function, including an example of how to display only upcoming events:

    https://theeventscalendar.com/knowledgebase/using-tribe_get_events/

    Will that help you get started? Please let me know. 🙂

    Cheers!
    Geoff

    #1156904
    Christian Thomson
    Participant

    Hi Geoff,

    Thanks for your reply. This is my loop:

    <?php if (have_posts()) : ?>
        <ul>
            <li>
                <div class="Date"><h3>Day / Date / Time</h3></div>
                <div class="Location"><h3>Course Location</h3></div>
                <div class="Cost"><h3>Cost</h3></div>
                <div class="Register"><h3>Registration</h3>
            </li>
    
    <?php while (have_posts()) : the_post(); ?>
            <li>
                <div class="Date">
                    <b><?php echo tribe_get_start_date($post->ID, true, $format = 'F j, Y - l' );?></b><br>
                    <?php echo tribe_get_start_date($post->ID, false, $format = 'g:i a' );?> to <?php echo tribe_get_end_date($post->ID, false, $format = 'g:i a' );?></div>
                <div class="Location"><b><?php echo tribe_get_venue()?></b><br><?php echo tribe_get_address()?></div>
                <div class="Cost"><?php echo tribe_get_cost($post->ID, true)?></div>
                <div class="Register"><a class="download_button" href="<?php the_permalink() ?>">Register</a></div> 
            </li>
        <?php endwhile; ?>
        </ul>    
    
    <?php else: ?>
        <?php include('courses-noavailable.php'); ?>
    <?php endif; ?>

    Do you think there’s a way to modify it?
    Thanks!
    Cintia
    [email protected]

    #1157052
    Geoff
    Member

    Thanks, Cintia!

    Is there a block of code above that that provides the query for posts? That where the type of post is being called and will give us an idea of how events are being queried and whether any changes can be made. 🙂

    Thanks a ton!
    Geoff

    #1157398
    Christian Thomson
    Participant

    Hi Geoff! Of course, I should have included that, on the page template for the Courses, I have several queries for the different categories:

    <?php 
                    if (is_page(413) ) { 
                    query_posts(array( 'post_type' => 'tribe_events' , 'tribe_events_cat' => 'occupational-first-aid-level-1', 'posts_per_page' => -1 ) ) ; 
                    include('courses-loop.php'); }
                
                    elseif (is_page(422) ) { 
                    query_posts(array( 'post_type' => 'tribe_events' , 'tribe_events_cat' => 'occupational-first-aid-level-2', 'posts_per_page' => -1 ) ) ;
                    include('courses-loop.php'); }
                      
                    elseif (is_page(424) ) { 
                    query_posts(array( 'post_type' => 'tribe_events' , 'tribe_events_cat' => 'occupational-first-aid-level-3', 'posts_per_page' => -1 ) ) ; 
                    include('courses-loop.php'); }

    and so on…

    Do you think we can modify so we can filter it? Or write the query differently?

    Thank you so much!
    Cintia

    #1157541
    Geoff
    Member

    Excellent! Thanks so much for the code, that helps a ton. 🙂

    See how query_posts is being used? That’s a totally valid way to query events — in fact, it’s the best way to query most WordPress posts.

    The problem with it in this case is that query_posts will fetch all events, regardless of whether they are past or upcoming and there is no filter to limit it only to upcoming events.

    Instead, I would suggest building a new query for those pages based of tribe_get_events instead. IT’s essentially the same thing as using query_posts but has additional parameters it accepts specifically for events, such as the ability to filter out past and upcoming events.

    This guide has the full documentation for tribe_get_events and also has great examples of how to use the function.

    Your query might look something like this:

    // Retrieve the next 5 upcoming events in occupational-first-aid-level-1 category
    $events = tribe_get_events( array(
    'posts_per_page' => 5,
    'tribe_events_cat' => 'occupational-first-aid-level-1',
    ) );
    
    // Loop through the events
    foreach ( $events as $event ) { ?>
    <li>
    <div class="Date">
    <b><?php echo tribe_get_start_date($post->ID, true, $format = 'F j, Y - l' );?></b><br>
    <?php echo tribe_get_start_date($post->ID, false, $format = 'g:i a' );?> to <?php echo tribe_get_end_date($post->ID, false, $format = 'g:i a' );?></div>
    <div class="Location"><b><?php echo tribe_get_venue()?></b><br><?php echo tribe_get_address()?></div>
    <div class="Cost"><?php echo tribe_get_cost($post->ID, true)?></div>
    <div class="Register"><a class="download_button" href="<?php the_permalink() ?>">Register</a></div>
    </li>
    <?php } ?>

    I haven’t tested that but hopefully will at least help you get started!

    Geoff

    #1158545
    Christian Thomson
    Participant

    Hi Geoff,

    Thanks again for the reply. I tried your code (modified it a bit), but I’m having a few problems:

    – The event start and end dates are showing today’s date and current time (see screenshot attached). And the Register button is linking to that Course’s page URL. Why is that?
    – The problem with this loop is that I can’t include the “else: include my no-courses-available.php” file (see my previous courses-loop.php file to see how my loop worked).

    I tested the changes on my local copy of the site. The code I inserted is:
    `<section class=”courses”>
    <?php
    if (is_page(413) ) {
    $events = tribe_get_events( array(
    ‘posts_per_page’ => -1,
    ‘start_date’ => date( ‘Y-m-d H:i:s’ ),
    ‘tribe_events_cat’ => ‘occupational-first-aid-level-1’,
    ) );?>
    <ul>
    <li>
    <div class=”Date”><h3>Day / Date / Time</h3></div>
    <div class=”Location”><h3>Course Location</h3></div>
    <div class=”Cost”><h3>Cost</h3></div>
    <div class=”Register”><h3>Registration</h3>
    </li>
    <?php foreach ( $events as $event ) {
    include(‘courses-loop.php’); } ?>
    </ul>
    <?php } ?>
    </div>`

    And my courses-loop.php code is changed to:

    `<li>
    <div class=”Date”>
    <b><?php echo tribe_get_start_date($post->ID, true, $format = ‘F j, Y – l’ );?></b><br>
    <?php echo tribe_get_start_date($post->ID, false, $format = ‘g:i a’ );?> to <?php echo tribe_get_end_date($post->ID, false, $format = ‘g:i a’ );?></div>
    <div class=”Location”><b><?php echo tribe_get_venue()?></b><br><?php echo tribe_get_address()?></div>
    <div class=”Cost”><?php echo tribe_get_cost($post->ID, true)?></div>
    <div class=”Register”><a class=”download_button” href=”<?php the_permalink() ?>”>Register</a></div>
    </li>`

    Can you help me figure out how to keep the functionality of the if/else (no course available) and also get the course dates to show correctly… and only when the course date is in the future?

    Thanks!!!

    #1158970
    Geoff
    Member

    Hey Cintia, thanks for following up!

    I’m afraid this is kind if custom development is beyond the scope of what we can provide here in the forums. I’m totally happy to keep this thread going and help point you in the right direction as best I can, but the specific development I’ll have to leave in your hands.

    That said, the if/else statement for no courses would have to be wrapped around the entire loop where the if is the code we’ve been working on so far and the else is what displays when no courses are available. Something like the examples documented here in the WordPress Codex.

    It also looks like the first half of your code is defining the start date as:

    ‘start_date’ => date( ‘Y-m-d H:i:s’ ),

    That would fetch the current date instead of what tribe_get_start_date() would fetch, which is the start date of the event post itself.

    I hope this helps! Let me know if you continue to hit troubles while working with the code.

    Thanks,
    Geoff

    #1167987
    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 ‘How to remove past events from list’ is closed to new replies.