By default, WP Event manager does not sort posted events by date. Instead, it sorts them by the order in which they are created. After a search on-line on how to sort events by date, I ran into the official post.  I could not help but notice how many thumbs down the post received even though it answered the question correctly.

My guess is that since it’s an more complex way to achieve the task at hand, it got some unfortunate down votes. In IT there are often different ways to accomplish the same tasks, each has nuanced effects and what may be good in one scenario, may not be good in another.

For most use cases, the easiest way to sort events by date is to change the short code on the viewing page. So unless you have a reason to change the PHP file and use the theme sorting, it’s best to take Occam’s razor into account and use the short code method.

In this post, I will show you how to sort by date using both methods.

 

How to Change WP Event Manager Sorting Using Shortcode

When you created your viewing page, you inserted a shortcode…. in it’s most simple form, the shortcode looks like this:

image

To sort by start date, simply insert the following lines into the shortcode:

orderby=”event_start_date” order=”ASC”

You can replace ASC with DESC if you want descending order instead of ascending order.

For example:

image

ASC will show the most recent events first while DESC will show the events that are further into the future first.

In the example below, the events are displayed in date order (nearest to farthest) using ASC.

 

Events

 

For more information and examples of sorting & filtering using WP Event Manager short codes, click here.

How to Change WP Event Manager Sorting by Editing the PHP File

This option will leave it up to the theme to do the sorting. First, find out what theme your site uses. To do this, go to Theme Options and you will see the theme name and version displayed as shown below.

image

Next we need to edit the theme’s function.php file to add the code for sorting by date.

Running VIM or NANO from the Linux shell is not most people’s cup of tea, but there are tools to facilitate editing PHP files.  One of these tools is the plug-in WP File manager; please install it if you don’t already have it.

To continue, click on the WP File Manager plug-in and locate the function.php file, it will be in wp-content-> themes –> theme name.

image

Highlight it and click on the duplicate icon to make a copy of the file (as a backup). This way, if something goes awry, you can delete the functions.php file you just edited, and rename the backup copy back to functions.php to get things back to normal.

image

Once you have made a copy and checked that it was created successfully (see image below), you may continue.

image

Next, highlight functions.php file and click on the edit file icon.

image

Copy and past the following code into the PHP text. You can change DESC to ASC depending on whether you want ascending or descending order.

 

/** You need to put code in theme functions.php **/

function theme_name_custom_orderby( $query_args ) {

$query_args[ ‘orderby’ ] = ‘meta_value’; //orderby will be according to data stored inside the particular meta key

$query_args[ ‘order’ ] = ‘DESC’;

return $query_args;

}

add_filter( ‘event_manager_get_listings_args’, ‘theme_name_custom_orderby’, 99 );

function theme_name_custom_orderby_query_args( $query_args ) {

$query_args[ ‘meta_key’ ] = ‘_event_start_date’; //here you can change your meta key

return $query_args;

}

add_filter( ‘get_event_listings_query_args’, ‘theme_name_custom_orderby_query_args’, 99 );

 

Insert the text after the developer’s initial comments. The original functions.php code will start something like this:

image

1. This is the starting function line, don’t change  it.

2. The /** indicates the start of the developer’s initial comments.

3. The lines starting with an asterisk indicate comments by the developer.

4. The */ indicates end of developer’s initial comments.

Add the copied code right after the initial set of comments, in our case in lines 7 or 8, but the line numbers may vary depending on the theme developer’s propensity to expatiate.

When finished adding the code, click save & close.

If these steps were followed correctly, the events will now be ordered by date.

Leave a comment

Your email address will not be published. Required fields are marked *

error: Sorry, copy/paste is disabled
Skip to content