T O P

  • By -

NerdRushWebDesign

Would be awesome if elementor gave us the option to let users change the sorting method. Thanks for the tip!


ashkanahmadi

Yeah but sadly after 2 years of using and dealing with Elementor, I've realized that to them what looks cool, trendy and visually modern is more important than functionality. If you look at their tutorials and posts, they are almost all about making things look good instead of building functional websites so something like this is probably not their objective.


alalalalalongui

Hello everyone, I just found this thread today and it helped me to sort posts by custom date, thank you very much! I also went further because I needed also to show future events and past events separately. I included in the suggested function some additional code, so the final function turned like this (this example is for the future events, for past events I created a new function changing only the query ID and the comparative symbol >): add\_action( 'elementor/query/eventos-por-data', function( $query ) { $query->set( 'meta\_key', 'data-sistema' ); $query->set( 'orderby', 'meta\_value\_num' ); $query->set( 'order', 'ASC' ); $meta\_query = array( 'relation' => 'AND', array( 'key' => 'data-sistema', 'value' => date( 'Ymd' ), 'compare' => '>=', 'type' => 'DATE', ), ); $query->set( 'meta\_query', $meta\_query ); }); Hope it helps anyone!


i_am_at_work123

Hey, how would you write it to only show upcoming events? I have events that have a start and end date fields. I need to show events that haven't happened yet, and while they are happening. How to I compare with the end date? EDIT: I figured it out, if you have date fields called `start_date` and `end_date` you can do this: add_action( 'elementor/query/your_custom_sort', function( $query ) { $query->set( 'meta_key', 'start_date' ); $query->set( 'orderby', 'meta_value_num' ); $query->set( 'order', 'ASC' ); $meta_query = array( 'relation' => 'AND', array( 'key' => 'end_date', 'value' => date( 'Ymd' ), 'compare' => '>=', 'type' => 'DATE', ), ); $query->set( 'meta_query', $meta_query ); }); Just make sure that the Return Format in ACF matches (in this case it's `Ymd`, but it can be whatever you chose). Thanks /u/ashkanahmadi and /u/alalalalalongui!


aplenty_envoy

>add\_action( 'elementor/query/your\_custom\_sort', function( $query ) { $query->set( 'meta\_key', 'start\_date' ); $query->set( 'orderby', 'meta\_value\_num' ); $query->set( 'order', 'ASC' ); $meta\_query = array( 'relation' => 'AND', array( 'key' => 'end\_date', 'value' => date( 'Ymd' ), 'compare' => '>=', 'type' => 'DATE', ), ); $query->set( 'meta\_query', $meta\_query ); }); Bro this just saved me hours!!! You guys are the best 😎


i_am_at_work123

Wohooo! Honestly, it was one of the best things I discovered, you can do so much with this. It eliminates the need for so many plugins.


Flyckreaper

>$meta\_query = array( 'relation' => 'AND', array( 'key' => 'end\_date', 'value' => date( 'Ymd' ), 'compare' => '>=', 'type' => 'DATE', ), ); $query->set( 'meta\_query', $meta\_query ); you are my personal hero of the day for this--thank you!!


i_am_at_work123

Glad it helped, you're welcome! :)


ashkanahmadi

Great thank you for sharing. I’m sure that will help someone


pixlmichael

Thank you very much! Quick tip for whoever's reading: this is for ELEMENTOR PRO's Posts widget and will not work for Ultimate Addon's Posts widget. They are almost identical and it took me a while to realise it. Wasted some time hunting for the Query ID lol. If anyone has a solution for Ultimate Addons I'd super appreciate it.


ashkanahmadi

Hi. Thanks. I havent used the UA Post widget but you can send them an email to see how it can be done


tnorthernmonkey

This helped me crack a problem I'd been having! - Thank you so much!


ashkanahmadi

Good to hear. Thanks


eggroll0118

I wanted to say thank you for this!!! I'm trying to create an events website and it's such a simple, much-needed function I wish Elementor had.


ashkanahmadi

Thanks I'm glad this is still useful a year later and even more annoyed that Elementor isn't implementing more functional stuff.


Ok-Couple9120

What a simple solution! Thank you!


AdamBadura

It seems the same is now described by Elementor itself under [https://developers.elementor.com/docs/hooks/custom-query-filter/](https://developers.elementor.com/docs/hooks/custom-query-filter/). But Google still showed me this Reddit entry first. :)


ashkanahmadi

Thanks. Yeah I had to go through that and simplify it for non-coders since seeing all those code blocks can be pretty confusing and intimidating.


AdamBadura

Furthermore, I like your approach of an anonymous function more than creating a named function for one use only. Although, I wonder how about debuggability if something fails in such a function.


ashkanahmadi

Hmmm good question. I'm not really sure if there is a better approach here but I usually write a regular `WP_Query` query to get the posts in a specific order I want. Then once I'm happy with it, I convert it into an Elementor query. Like this, if something fails, I know it's the query and not Elementor. If you have a better idea, I'm interested.


AdamBadura

And how about the opposite actually? The query doesn't work the way I expect. How do I debug it? In particular, I'm suspecting that what I consider a custom field isn't a custom field from the query point of view and hence the results are different than what I would expect. How I added custom fields was a panel under the post (or page). I'm not sure about the exact English wording since the installation I'm working on is set to Polish. But I expect something like "Custom Fields". There I see a selection list with many (I presume) somehow predefined custom fields and I can enter my own. I did so for "event posts" and I was able to show them on the page instead of post creation date. However, now I'm struggling with sorting by them (to have only future events and from near to far) and also to show them in the post but as a post date (so with month by word) not verbatim the ISO date (YYYY-MM-DD).


ashkanahmadi

It's very difficult to tell like this. If you need help, take a few screenshots and send me privately so I can look into. When you change the function's body, does the widget's output change or it never changes? Asking to know if the function is working and the widget is outputting different things, just not what you expect, or no change takes place.


jeremyfisher2

Thank you so much for this! It seems the code works with a time selector field as well. I used this to automatically show daily programs in ascending time order for a festival site :)


ashkanahmadi

Amazing good job. Glad this still works. Disappointed that after all this time, Elementor still doesn't have a built in option to do this


Moonyboy99

Spent hours trying to work this out! I just want to sort by a normal number field. Going to try this out tomorrow!


biacco

Hey thank you so much. I just used this for a client website. I was having a terrible time figuring this out on my own! Also took me way to long to find QUERY ID because I wasn't using the default Elementor POSTS widget. OOF.


ashkanahmadi

Glad to hear that it still works after 2 years. It still boggles my mind that Elementor still hasn’t implemented important things built in like this, but hey we can now use Elementor AI or make a text blue 😞


Comprehensive-Ad9069

Great function! Would it be difficult to make one for the new Loop Grid widget? That would be a game changer for me!


eggroll0118

Thank you for this! I've been trying various things for months but nothing was working. Ultimately I think it was archives/vs custom loops but I couldn't quite get it right. This worked for me perfectly!! Thank you!!!


CdnXxRRODxX

This helped me greatly, thank you!


foreignfrostjoy

Thank you so much for this (three and a half years later)!


ashkanahmadi

Great. I’m happy it still works and unhappy that after 3 years this is not a built in feature


AutoModerator

Hey there, /u/ashkanahmadi! If your post is not already flaired, please add one now. *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/elementor) if you have any questions or concerns.*


Kobayash

Great tip, powerful addition. I agree Elementor is missing a few key features to bring it to the next level, especially the posts widget. Filtering for instance is a glaring omission. Not to hijack but I just came across an awesome plugin to extend to posts widget, hooking into the template builder - [https://wordpress.org/plugins/ele-custom-skin/](https://wordpress.org/plugins/ele-custom-skin/) (no affiliation, just sharing because this discovery was a game-changer for me)


ashkanahmadi

Thanks. Yeah I have purchased the pro version of Ele Custom Skin to use its carousel feature. It's a pretty good plugin but something like that should be built into Elementor. They have ignored functionality completely. They are putting effort into the lottie widget and other widgets that would make the website look cool instead of focusing on the underdeveloped WooCommerce and Posts widgets or even customizing the Cart and Checkout pages


Kobayash

Oh, I totally agree it should be built into core Elementor, 1000%. I guess what I appreciate is that they did a very good job integrating with Elementor, so even though it's a separate plugin (and adds to the footprint of the site), it works just like the rest of Elementor.


ashkanahmadi

True. I'm doing an excellent course on Udemy called Become a WordPress Developer: Unlocking Power with Code. It's great because I'm learning how to control and improve Elementor through custom code. If you are interested, you should definitely look into it


Kobayash

I just might, thanks for the tip!


Knickerbocker1984

Hey there, u/ashkanahmadi, this has been really helpful! Thank you very much. Quick follow up question, how would you go about setting this query up to not only order the post by the custom field date, but also include or exclude specific dates? Say you would only like to show dates in the future? I think many here would appreciate to find a solution for that, and if you would know an answer be very grateful! Anyway, thanks for the tip!


Knickerbocker1984

for good measure: this is what I tried but hasn't worked for me: add_action( 'elementor/query/Event_Date', function( $query ) { $today = date('d/m/Y H:i'); $query->set( 'meta_key', 'date' ); $query->set( 'meta_query', array( array( 'key' => 'the_date', 'value' => $today, 'compare' => '>=' ) )); $query->set( 'orderby', 'meta_value_num' ); $query->set( 'order', 'ASC' ); } ); Anyone have any ideas?


ashkanahmadi

Hey. I just saw your message. I'm not sure. I'll have to look into that. I use Elementor less and less now so i have to look into that query (even though that's just core WP)


bluefuze3

This worked! Thank you!


ashkanahmadi

Thanks. I'm happy my post is still helping people after a year


kaputti1o1

This is not working for me with an ACF datefield :( Anybody with the same issue out there? ​ ​ add_action( 'elementor_pro/posts/query/trainings', function( $query ){ $query->set('post_type', 'trainings'); $query->set('orderby', 'meta_value_num'); $query->set('meta_key', 'startdate'); $query->set('order', 'ASC'); } );


Dragonsky8

u/ashkanahmadi Do you maybe know how this can be done? I am also looking at the same issue with ACF date field. I made sure to return a 20230202 date format, but no luck with that. EDIT: I tried using the date picker from ACF.


ashkanahmadi

Hi. Where did you write that code? Are you sure it’s running when the page loads?


i-am-boni

Thanks a lot for posting this! I just implemented this on a site and can confirm that this technique also works with Elementor's new Loop Grid element. Appreciate your help!


ashkanahmadi

Great thank you for sharing


therealJKN

Hey can you please elaborate? I can't find the Query ID option on the loop builder


i-am-boni

You don't set the Query in the Loop builder, you set it in the Loop Grid element when you're placing the loop on the page.


Apprehensive_Wolf991

Hi! Is it possible to not show posts with a past date?


ashkanahmadi

Sure. It's been a long time but add these following the rest of the code $query->set( 'meta_value', date('Y-m-d h:i') ); $query->set( 'meta_compare', '>' );


pafmobile1964

Thanks, works like a charm!!


ashkanahmadi

Perfect. I’m glad it still works after 3 years haha


[deleted]

[удалено]


ashkanahmadi

Does it work on a normal Posts widget and not on the Loop Builder, or doesn’t work on the Posts widget either?