WordPress.org

Ready to get started?Download WordPress

Forums

Events Manager
[resolved] Showing only future events and those that ended at most X hours ago (12 posts)

  1. Daedalon
    Member
    Posted 2 years ago #

    The default scope 'future' shows all events that start or end today or later. This creates a situation where when looking at the event list in the evening, the first thing a person sees are events that ended right after the midnight, some 20 hours ago.

    I'd like to create a custom scope that acts otherwise the same, but doesn't display events that have ended more than X hours ago. With the instructions for creating a custom scope I have solved hopefully every other part of the issue except of the correct way to reference the ending time of the event in hours, as the example code handles only full days:

    $start_date = date('Y-m-d',current_time('timestamp'));
    		$end_date = date('Y-m-d',strtotime("+1 day", current_time('timestamp')));
    		$conditions['scope'] = " (event_start_date BETWEEN CAST('$start_date' AS DATE) AND CAST('$end_date' AS DATE)) OR (event_end_date BETWEEN CAST('$end_date' AS DATE) AND CAST('$start_date' AS DATE))";

    http://wordpress.org/extend/plugins/events-manager/

  2. agelonwl
    Member
    Posted 2 years ago #

    you can add event_start_time and event_end_time there then compare against x hours

  3. Daedalon
    Member
    Posted 2 years ago #

    Thanks for the quick reply. The thing I'm missing is the correct formatting for that code, as the example quoted above uses a syntax of CAST('$start_date' AS DATE) that I don't know how to modify to support time of the day in addition to just the date.

    Possibly the only part I'm missing is how to turn "CAST .. AS DATE" into a suitable command for time in the format Events Manager uses it. "CAST .. AS DATETIME"?

  4. Daedalon
    Member
    Posted 2 years ago #

    Aglonwl, do you have any tips on the syntax for comparing hours to the event_start/end_date in the database?

  5. Daedalon
    Member
    Posted 2 years ago #

    Reading http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html#function_cast it seems that CAST ... AS TIME is the way to go.

  6. treysea
    Member
    Posted 2 years ago #

    Hi Daedalon - I am having the same problem with the future scope. Were you able to get a custom scope working?

  7. Daedalon
    Member
    Posted 2 years ago #

    I got the exact future scope working that I was going for. I can dig it up for you if you'd like to use the same, but there's a plugin setting that might solve the problem for you immediately. It was called something like "Show current events as future events", the meaning of which I didn't understand, but after having a look at the code I figured out it means "show ongoing events that started on earlier days among upcoming events of the date".

    What turning off the setting precisely does is to hide from today's view all events that were started on an earlier event despite them ending today or on a future date. This is exactly what's needed in many cases and makes one hope for a more clear name and explanation of the setting.

    What my custom scope does is that it shows those earlier events that haven't already ended more than X hours ago, and also shows of today's events only those that haven't already ended more than X hours ago. Using the setting X = 1 works well, but we'll later either start using 0 or make our custom walker that separates events in three groups: upcoming, ongoing, and recently ended.

  8. Daedalon
    Member
    Posted 2 years ago #

    Here's the code. The SQL is broken on multiple lines for readability, but is technically just one big line, making this a 10-line piece of code plus the comment.

    // Future and X hours ago aka 'mycustomfuture'
    add_filter( 'em_events_build_sql_conditions', 'my_em_scope_mycustomfuture_conditions',1,2);
    function my_em_scope_mycustomfuture_conditions($conditions, $args){
    	if( !empty($args['scope']) && $args['scope']=='mycustomfuture' ){
    		$start_date = date('Y-m-d',current_time('timestamp'));
    		$today = $start_date;
    		$end_time = date('H:i:s', strtotime("-1 hour", current_time('timestamp')));
    		$conditions['scope'] = " (recurrence = 0 OR recurrence IS NULL)
    			AND (
    				event_start_date > CAST('$today' AS DATE)
    				OR (
    					event_start_date = CAST('$today' AS DATE)
    					AND
    					event_end_date = CAST('$today' AS DATE)
    					AND
    					event_end_time >= CAST('$end_time' AS TIME)
    				)
    				OR (
    					(
    						event_end_date != '0000-00-00'
    						AND event_end_date IS NOT NULL
    					)
    					AND (
    						event_end_date > CAST('$today' AS DATE)
    						OR
    						(
    							event_end_date = CAST('$today' AS DATE)
    							AND
    							event_end_time >= CAST('$end_time' AS TIME)
    						)
    					)
    				)
    			)";
    	}
    	return $conditions;
    }

    A short description of the SQL (the $conditions['scope'] = ... line):

    1. Exclude the master templates of recurrences AND match any one of the following three:
    2. Event starts on a date later than today OR
    3. Event starts today and ends today and has not ended already more than X hours ago OR
    4. Event start date is whatever, except unset, AND: event either ends on a date later than today OR event ends today and has not ended already more than X hours ago.

    That last one catches events that start today and end tomorrow, or those that started yesterday and end up later today, for example.

    The part where to change what's the earliest end date that is accepted, it's the $end_time = ... line. "-1 hour" means one hour ago. Notice that this expression doesn't take (or at least need) plurals - use "-2 hour", not "-2 hours".

  9. Daedalon
    Member
    Posted 2 years ago #

    The above code was missing the ability to show all day events of the same date. The code below fixes this with the last OR statement. The formatting of the code is also improved.

    // Future and X hours ago aka 'mycustomfuture'
    add_filter( 'em_events_build_sql_conditions', 'my_em_scope_mycustomfuture_conditions',1,2);
    function my_em_scope_mycustomfuture_conditions($conditions, $args){
    	if( !empty($args['scope']) && $args['scope']=='mycustomfuture' ){
    		$start_date = date('Y-m-d',current_time('timestamp'));
    		$today = $start_date;
    		$end_time = date('H:i:s', strtotime("-1 hour", current_time('timestamp')));
    		$conditions['scope'] = " (recurrence = 0 OR recurrence IS NULL)
    			AND (
    				event_start_date > CAST('$today' AS DATE)
    				OR (
    					event_start_date = CAST('$today' AS DATE)
    					AND
    					event_end_date = CAST('$today' AS DATE)
    					AND
    					event_end_time >= CAST('$end_time' AS TIME)
    				)
    				OR (
    					(
    						event_end_date != '0000-00-00'
    						AND
    						event_end_date IS NOT NULL
    					)
    					AND (
    						event_end_date > CAST('$today' AS DATE)
    						OR
    						(
    							event_end_date = CAST('$today' AS DATE)
    							AND
    							event_end_time >= CAST('$end_time' AS TIME)
    						)
    					)
    				)
    				OR (
    					event_start_date = CAST('$today' AS DATE)
    					AND
    					event_all_day = '1'
    				)
    			)
    		";
    	}
    	return $conditions;
    }
  10. treysea
    Member
    Posted 2 years ago #

    Hi - thanks for your response.

    I've read over your posts and the tutorial on the EM site, but I'm just not sure where this code would go. I am trying to affect the scope of a sidebar widget. Where do you define a custom scope so that it is upgrade proof?

    Thanks again.

  11. Daedalon
    Member
    Posted 1 year ago #

    You would have to make your own plugin. http://wp-events-plugin.com/tutorials/create-your-own-event-scope/ doesn't unfortunately have the complete information for that, but isn't missing much.

    1. Make a directory wp-content/plugins/events-manager-my-customizations and there a file events-manager-my-customizations.php.
    2. Insert the code there as in the above link, add the code in my latest paste to it and make the following addition as well to activate this new code. The bolded parts are what you need to add, that is the comma in the end of line 4 and the following unnumbered line, and everything else was copy-pasted from the link above.

      1 add_filter( 'em_get_scopes','my_em_scopes',1,1);
      2 function my_em_scopes($scopes){
      3 $my_scopes = array(
      4 'today-tomorrow' => 'Today and Tomorrow',
      'mycustomfuture' => 'Your description, shown in Events Manager settings in the default scope dropdown list'

      5 );
      6 return $scopes + $my_scopes;
      7 }

    3. Activate the plugin on your wp-admin/plugins.php page.

    The name "my_em_scope_mycustomfuture_conditions" can be changed, as long as you change it on both lines, the one that starts with add_filter and the one that starts with function. The name 'mycustomfuture' can be changed, as long as it is also changed in two places: inside the function on line if( !empty($args['scope']) && $args['scope']=='mycustomfuture' ){ and inside the add_filter declaration that is copy-pasted above in this post.

    That's all it takes to add a new scope: make a plugin, copy-paste a scope there, rename it and start modifying.

  12. Daedalon
    Member
    Posted 1 year ago #

    Here's a version that fixes the bug of some events being hidden for a while after midnight. The main meaningful changeable value is now controlled by the $grave_period variable for easy changing. The SQL is also greatly simplified. If any bugs occur that I can reproduce I'll gladly fix them.

    // Future and X hours ago aka 'mycustomfuture'
    add_filter( 'em_events_build_sql_conditions', 'my_em_scope_mycustomfuture_conditions',1,2);
    function my_em_scope_mycustomfuture_conditions($conditions, $args){
    	$grace_period = '1 hour'; // How long to show events after ending
    	if( !empty($args['scope']) && $args['scope']=='mycustomfuture' ){
    		$start_date = date('Y-m-d',current_time('timestamp'));
    		$today = $start_date;
    		if ( date( 'd', strtotime( 'now' ) ) != date( 'd', strtotime( '-' . $grace_period, 'now' ) ) ) {
    			$end_time = '00:00:00';
    		} else {
    			$end_time = date( 'H:i:s', strtotime( '-' . $grace_period, current_time('timestamp') ) );
    		}
    		$conditions['scope'] = " (recurrence = 0 OR recurrence IS NULL)
    			AND (
    				event_start_date > CAST('$today' AS DATE)
    				OR
    				event_end_date > CAST('$today' AS DATE)
    				OR (
    					event_end_date = CAST('$today' AS DATE)
    					AND (
    						event_all_day = '1'
    						OR
    						event_end_time >= CAST('$end_time' AS TIME)
    					)
    				)
    			)
    		";
    	}
    	return $conditions;
    }

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.