Home › Forums › Calendar Products › Events Calendar PRO › Order by End Date
- This topic has 5 replies, 2 voices, and was last updated 11 years ago by
George.
-
AuthorPosts
-
April 21, 2015 at 7:18 am #957023
Neasa Ronayne
ParticipantI still haven’t found a solution to being able to order the events archive by end date, I have used meta_value and meta_key but it orders it like a string rather than an integer. I then used meta_value_num but to no success.
Any advice on how to get this to work?
See my previous thread:
philipfarmer
April 20, 2015| Permalink
Hi I am looking to order by events by end-date. I have read a number of posts that reference tribe_events_query_posts_orderby, but I can’t find this in the technical documentation to see the structure of this hook.
I have also read up on using the wp pre_get_posts. This is what I have implemented:function event_title_sort($query) {
if ( !is_admin() && ( $query->tribe_is_event || $query->tribe_is_event_category ) ) {
$query->set( ‘meta_key’,’EventEndDate’ );
$query->set( ‘orderby’,’meta_value_num’ );
}
}
add_action( ‘pre_get_posts’, ‘event_title_sort’, 100, 1 );
This seems to order the posts by end date but it seems to be ordering it by meta_value (1,12,2,3,34,9) rather than meta_value_num (1,2,3,9,12,34), it also changes the start date to match the end date (this change is only to the text appearing not the actual event start date)Any suggestions or solutions would be much appreciated.
Thanks.Posts: 4514
Brian
April 20, 2015| Permalink
Hi,
Thanks for using our plugin.
The plugin is designed to order events by the start date and time so changing this could be tricky and create unintended issues.
However, we use a couple different ones for order:
meta_value
meta_key
Do either of those work?Posts: 4
Topics: 3
philipfarmer
April 21, 2015| Permalink
Hi Brian,
Thanks for the quick response. No worries I understand any help would be much appreciated.
As I previously stated I have used both those. They seemed to order the events 1,12,3,9 rather than 1,3,9,12. So I tried meta_value_num to solve this issue but it remains the same.
As a test I tried these to order the events by title and it worked fine, the issue seems to be that I am dealing with numbers.
CheersPosts: 4514
Brian
April 21, 2015| Permalink
Ok thanks for the information. Glad you are able to move forward on your customization.
Since there is nothing more we can do here on a customization I am closing out this ticket, but if you have any other issues related to this or something new, please create a new one.
ThanksApril 21, 2015 at 8:39 am #957057George
ParticipantHey Philip,
Sorry for the frustration you’ve been having here. Brian is correct about us only being able to provide very limited support for customizations, which I know isn’t great news, but the thing with this sort of customization regardless is that the issue you’re describing is pretty tricky to work around – in other words, regardless of our customization support policies, when there are strings trying to represent numeric values, WordPress’ orderby parameters don’t give you much control over how exactly they compare values.
This is something hinted at even in the WordPress Codex itself, check out this screenshot → https://cloudup.com/cnRzDscupj0
Working around the weirdness found within meta_value and/or meta_value_num is tricky.
So, there are two things I can personally think of here.
1. First, delve into the WP_Meta_Query class, and try writing a more complex/nuanced meta-based query within the pre_get_posts() query you’ve written here. Check out this Codex page for more information → https://codex.wordpress.org/Class_Reference/WP_Meta_Query
2. Learn to write custom $wpdb queries directly, and write one that queries things and sorts by end date values if possible. You can learn about this here → https://codex.wordpress.org/Class_Reference/wpdb
I don’t necessarily if #2 here would be very helpful, and am sorry for the limitations here Philip, but it’s indeed a tricky thing to do. I’m curious – are you able to totally sort by start date 100% fine, with no issue? Even with the same meta_name and various meta-related orderby parameters you try here with the End date?
— George
April 22, 2015 at 2:08 am #957269Neasa Ronayne
ParticipantGreat, Thanks George. I will try those and get back to you whether it works or not.
Thanks
April 22, 2015 at 2:57 am #957276Neasa Ronayne
ParticipantPS
Sorry I didn’t answer your question, if I change the meta_key to start date the events are ordered 100% fine.April 23, 2015 at 2:15 am #957624Neasa Ronayne
ParticipantHi Geeorge/All,
I managed to the re-ordering of events by creating a querying on a page rather than using the pre_get_posts in the functions.php
This solution is adequate for me but may not be for everyone, here is my code if you want to save/pass along:
$args = array( 'meta_key'=> '_EventEndDate', 'meta_type'=>'DATE', 'orderby'=>'meta_value_num', 'post_type'=>'tribe_events' ); $the_query = new WP_Query( $args ); while ($the_query->have_posts()) : $the_query->the_post(); endwhile; wp_reset_query();Just add this to any php file you want the events to appear. An error with this script is the start and end date appear the same.
Good Luck.April 23, 2015 at 8:57 am #957768George
ParticipantThanks for the update and for sharing what worked for you Philip!
Best of luck with your project!
George -
AuthorPosts
- The topic ‘Order by End Date’ is closed to new replies.
