Only show past events from a certain year

Home Forums Calendar Products Events Calendar PRO Only show past events from a certain year

Viewing 15 posts - 1 through 15 (of 17 total)
  • Author
    Posts
  • #1214215
    Mogul
    Participant

    Hi,

    I am trying to create a sort of archiving past events system, its only by years, so up to now i have /auctions/photo/?tribe_event_display=past which shows all past events, but when i try /auctions/photo/?tribe_event_display=past&tribe-bar-date=2016 i want to only show 2016 events, and /auctions/photo/?tribe_event_display=past&tribe-bar-date=2017 should show nothing now till an event has been and past.

    Is there a way to do this?

    #1214219
    Mogul
    Participant

    I have found this from the documentation:

    // Retrieve all events in 2016
    $events = tribe_get_events( array(
    ‘eventDisplay’ => ‘custom’,
    ‘start_date’ => ‘2016-01-01 00:01’,
    ‘end_date’ => ‘2016-12-31 23:59’
    ) );

    How do i show this in the front end? Do i need to setup a new page template, or is there a better way?

    thanks

    #1214225
    Mogul
    Participant

    I found this snippet on this thread: https://theeventscalendar.com/support/forums/topic/event-search-bar-date-function/

    /* Tribe, limit tribe bar date search to one day */
    add_action( 'tribe_events_pre_get_posts', function( $query ) { 
        $ajax_or_main = ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || $query->is_main_query();
     
        if ( ! $ajax_or_main || empty( $_REQUEST['tribe-bar-date'] ) ) {
            return;
        }
     
        if ( ! preg_match( '/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $_REQUEST['tribe-bar-date'], $matches ) ) {
            return;
        }
     
        $end_date = date('Y-m-d', strtotime( $matches[0] . ' +1 day') );
     
        $query->set( 'end_date', $end_date);
    } );

    Is there a way to edit this so it shows just 1 year?

    #1214510
    Josh
    Participant

    Hey Mogul,

    Thanks for reaching out to us!

    The snippet you found above has elements that could help here.

    To build on that snippet, what we could do would be to check if the “tribe_bar_date” that is set is just a year (like you have as 2016 in your first post above.)

    If so, you can “$query->set” both the start and end date to the beginning and the end of the year (like you have in the second post above).

    Let me know if this helps.

    Thanks!

    #1214777
    Jeff
    Participant

    I wrote the code below to show events from the start to end of each year, then in the table I show all the events. However only the events which haven’t passed have hyperlinks to the event. The past events just show as text. I’m a complete newbie to this so I might be wrong in how I’m doing it, but it works. If Josh says ignore my code, then ignore it. You can also take out all the commented code. It was useful for me to learn so I left it in.

    <div id="schedule">
    <table border="0" cellpadding="3">
    <tbody>
    <?php
    	global $post;	// Ensure the global $post variable is in scope
    	$row = 0;
    	$events = tribe_get_events( array(
    		'start_date'     => date( '2016-01-01 12:00:00' ),
    		'end_date'       => date( '2026-12-31 12:00:00' ),
    		'eventDisplay'   => 'custom',
    		'posts_per_page' => -1
    	));
    	$todaysDate = date("Y-m-d H:i:s");
        if ( empty( $events ) ) {
    		echo 'Sorry, nothing found.';
    	}
    	
    	// Or we may have some to show
    	else foreach( $events as $post ) {
    		setup_postdata( $post );
    		//echo get_the_title( $post ) . '--Jeff'.tribe_get_start_date().'<br/>';
    		//echo "*".tribe_get_start_date(null,false, "Y-m-d H:i:s");
    		//$eventDate=get_post_meta($id,'_EventStartDate', "Y-m-d H:i:s");
    		//if ($eventDate >= $todayDate) {
    			//echo "*";
    		//}
            if ($row%2 == 0) {
    			echo "<tr class=\"opposite\">".PHP_EOL;
    		} else {
    			echo "<tr class=\"normal\">".PHP_EOL;
    		}
    		$eventDate = tribe_get_end_date(null,true,"Y-m-d H:i:s");
    		//echo $todaysDate."  ".$eventDate;
    		$state = tribe_get_state();
    		if(empty($state)) {
    			$locationStringJeff = tribe_get_city().', '.tribe_get_country();
    		} else {
    			if (tribe_get_country()=='United States') {
    				$locationStringJeff = tribe_get_city().', '.tribe_get_state().', USA';
    			} else {
    				$locationStringJeff = tribe_get_city().', '.tribe_get_state().', '.tribe_get_country();
    			}
    		}
    		if (tribe_events_has_tickets()) {
    			$starString = "*";
    			$textColor = "#000000";
    		} else {
    			$starString = "";
    			$textColor = "#777777";
    		}
    		$position = strpos(get_the_title(), "–");
    		$title_text = substr(get_the_title(), 0, $position); //This removes text after the dash
    		$major_city_text = substr(get_the_title(), $position);  //Ali wants to show the major city like Chicago so since this is already in the title why not...
    		
    		if ($eventDate <= $todaysDate) { ?>
    			<td class="date"><?php echo tribe_get_start_date(null,false,'M j').'-'.tribe_get_end_date(null,false, 'j, Y').$starString; ?></td><td class="location" style="color:<?php echo $textColor?>;"><?php echo $major_city_text ?></td><td class="level"><?php echo $title_text;?></td>
                <?php
    		} else { ?>
            	<td class="date"><a href="<?php echo esc_url( tribe_get_event_link() ); ?>" title="<?php the_title_attribute() ?>" rel="bookmark"><?php echo tribe_get_start_date(null,false,'M j').'-'.tribe_get_end_date(null,false, 'j, Y').$starString; ?></a></td><td class="location" style="color:<?php echo $textColor?>;"><?php echo $major_city_text ?></td><td class="level"><?php echo $title_text;?></td>
            <?php
            }
    		//echo tribe_events_get_the_excerpt ();	//Playing around with getting the text from the main event description -- This is a tribe event
    		//echo the_content();	//Getting the text from the main event using WordPress function...allows html, but then also inserts images which I won't want in an RSVP email...
            echo "</tr>". PHP_EOL;
    		$row++;
    	} 
    	//php echo tribe_get_city().', '.tribe_get_state();
    	//echo $title_text;
    ?>
    
    </tbody>
    </table>
    </div>
    #1215015
    Mogul
    Participant

    Really, i just want to use a link like /auctions/photo/?tribe_event_display=past&tribe-bar-date=2016 and it just shows all events in 2016 or /auctions/photo/?tribe_event_display=past&tribe-bar-date=2015 and it shows all events in 2015.

    At the moment /auctions/photo/?tribe_event_display=past&tribe-bar-date=2015, shows 2015 and then 2016.

    the code i am using atm is:

    add_action( 'tribe_events_pre_get_posts', function( $query ) { 
        $ajax_or_main = ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || $query->is_main_query();
     
        if ( ! $ajax_or_main || empty( $_REQUEST['tribe-bar-date'] ) ) {
            return;
        }
     
        if ( ! preg_match( '/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $_REQUEST['tribe-bar-date'], $matches ) ) {
            return;
        }
     
        $end_date = date('Y-m-d', strtotime( $matches[0] . ' +1 year') );
     
        $query->set( 'end_date', $end_date);
    } );

    How can i amend that to do what i need?

    thanks

    • This reply was modified 9 years, 4 months ago by Mogul.
    #1215051
    Jeff
    Participant

    Your question isn’t about Events Calendar, it’s about passing variables from a url via GET and then processing those variables in your php script. It’s very easy to do with a normal, non-wordpress, website. And the info can be found at: http://html.net/tutorials/php/lesson10.php

    Again in a normal website you could have your url be like this:
    /auctions/photo/list_events_page.php?start_date=2016-01-01&end_date=2016-12-31

    Then on the list_events_page.php page you would have code like this:
    <?php
    $start = $_GET[“start_date”]; //Pulls the info from the URL and puts it in the $start variable
    $end = $_GET[“end_date”] //Same here
    $events = tribe_get_events( array(
    ‘start_date’ => date($start),
    ‘end_date’ => date($end),
    ‘eventDisplay’ => ‘custom’,
    ‘posts_per_page’ => -1
    ));

    ?>
    Then use some of the other code above to display what you want from the events in the loop that I used to setup a nice looking tabular format for your information.

    However, you’re using WordPress which makes these variables within the url more of a hassle. I only looked into it briefly and here’s a link describing a few methods of passing these variables.
    http://stackoverflow.com/questions/4586835/how-to-pass-extra-variables-in-url-with-wordpress

    Hope this helps.

    #1216222
    Josh
    Participant

    Hey Jeff,

    Thanks for adding that here! Some great stuff there and looks like it would be a great solution for people looking for a similar thing here.

    Mogul,

    If you don’t want to go with the template type solution Jeff has provided here, take a look at this quick structure that follows the snippet you found above. This isn’t a final, tested snippet but gives you the starting point for how to pull this together:

    https://gist.github.com/BeardedGinger/0f1b5aa69df8479876f1fbbe963cdd47

    #1217500
    Mogul
    Participant

    that seems to sort of work, is there a way to make it specific for past events, so it doesn’t pick up upcoming events?

    thanks

    #1217837
    Josh
    Participant

    Hey Mogul,

    Sure thing!

    When doing the “$query->set()” arguments, you can set the eventDisplay argument to past:

    
    $query->set( 'eventDisplay', 'past' );
    
    

    And that should just pull in past events into the results.

    Let me know if this helps.

    Thanks!

    #1217905
    Mogul
    Participant

    Thanks, how would you see the url for that piece of code?

    At the moment i am using: /auctions/photo/?tribe_event_display=past&tribe-bar-date=2016-01-01 which shows the only event in 2015, which was the first event on the site,

    Then /auctions/photo/?tribe_event_display=past&tribe-bar-date=2017-01-01 shows the events in 2016 and also the only event in 2015 which i don’t want to appear.

    thanks

    • This reply was modified 9 years, 4 months ago by Mogul.
    #1219339
    Josh
    Participant

    Hey Mogul,

    The snippet:

    https://gist.github.com/BeardedGinger/0f1b5aa69df8479876f1fbbe963cdd47

    Assumes a tribe-bar-date of just the year “2016” or “2017”, try modifying the URL for that and see if it helps.

    Thanks!

    #1219495
    Mogul
    Participant

    Thanks,

    /auctions/photo/?tribe_event_display=past&tribe-bar-date=2016 still shows all past events including yesterdays.

    /auctions/photo/?tribe_event_display=past&tribe-bar-date=2016 shows exactly the same as the 2016 url, where it should only show yesterdays event

    #1220744
    Josh
    Participant

    Hey Mogul,

    Thanks for following up.

    It sounds like the “eventDisplay =>’past'” is overriding the original year based approach we came up with here:

    https://gist.github.com/BeardedGinger/0f1b5aa69df8479876f1fbbe963cdd47

    As an alternative, you could further extend the above snippet to check if the provided “tribe-bar-date” is the current year. If so, you could use a different end date of the date() so it won’t include events all the way up to the end of the current year.

    Let me know if this helps.

    Thanks!

    #1228791
    Mogul
    Participant

    Still cannot get the above working, I had a thought of doing it with categories, but when i do the url /auctions/category/2017/

    I don’t get any auctions as they have past, is there anyway to add past auctions to show on categories?

Viewing 15 posts - 1 through 15 (of 17 total)
  • The topic ‘Only show past events from a certain year’ is closed to new replies.