- This topic has 15 replies, 5 voices, and was last updated 3 years, 2 months ago by
Fernando.
-
AuthorPosts
-
November 8, 2022 at 2:06 am #2405900
Nick
Hi,
I’ve got a custom post type setup for Events and using ACF for a date field for the event. I’ve looked at displaying upcoming events with the GB Query Loop block which works great. But I need to order the posts by the SCF Date field. E.G. For the events page to show the next upcoming date as opposed to the post publish date.
Can you point me in the right direction? I’ve seen a few other posts on the forum relating to this but don’t exactly match my situation. Is there a function I can add to my theme to get the Query Loop to allow the Order By parameter for the ACF Date field?
Thanks,
Nick.November 8, 2022 at 3:02 am #2405953David
StaffCustomer SupportHi there,
try this PHP Snippet:
add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) { // apply filter if loop has class: my-class-name if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'my-class-name' ) !== false ) { // merge meta_key my-custom-field into query return array_merge( $query_args, array( 'meta_key' => 'my_custom_field', 'orderby' => 'meta_value', 'order' => 'DESC', ) ); } return $query_args; }, 10, 2 );Notes: in the code:
1.
my-class-namechange this to a CSS Class name of meaning eg.in-date-orderwhich you need to add to the Query Loop > Grid Blocks –> Advanced -> Additional CSS Class(es) field.2.
my_custom_fieldneeds to be changed in the code to the name of your ACF Custom field.November 8, 2022 at 6:04 am #2406154Nick
Thanks for your response David.
I’ve just added that to my functions.php and edited. But it doesn’t appear to have any effect.
I should have said I’m using the ACF ‘Date Time Picker’ field type named ‘date_time’ with the format for display and return set to
l F j, Y g:i a
Below is a copy of the functions code and the css class ‘in-date-order’ is applied to the query loop grid. Do I need to edit the orderby and order properties?
Thanks,
Nick.add_filter( ‘generateblocks_query_loop_args’, function( $query_args, $attributes ) {
// apply filter if loop has class: my-class-name
if (
! empty( $attributes[‘className’] ) &&
strpos( $attributes[‘className’], ‘in-date-order’ ) !== false
) {
// merge meta_key my-custom-field into query
return array_merge( $query_args, array(
‘meta_key’ => ‘date_time’,
‘orderby’ => ‘meta_value’,
‘order’ => ‘DESC’,
) );
}return $query_args;
}, 10, 2 );
November 8, 2022 at 6:25 am #2406193David
StaffCustomer SupportTry this:
add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) { // apply filter if loop has class: my-class-name if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'in-date-order' ) !== false ) { // merge meta_key my-custom-field into query return array_merge( $query_args, array( 'meta_key' => 'date_time', 'meta_type' => 'DATETIME', 'orderby' => 'meta_value', 'order' => 'DESC', ) ); } return $query_args; }, 10, 2 );That includes the
'meta_type' => 'DATETIME',which should handle that type.November 8, 2022 at 7:36 am #2406309Nick
Wahey!
Thanks David. That worked a charm.
Also found a way to automate expiry of events to set them from an Upcoming to Past categories and display them on relevant pages with the query loop filtering them accordingly. PublishPress Future. Seem to work quite well to be able to set an expiry date.
Cheers.
November 8, 2022 at 8:46 am #2406658David
StaffCustomer SupportAwesome – glad to hear that!
December 28, 2022 at 1:46 pm #2476168John
Hi David,
If I may piggyback on this thread, I have the exact same need, but my date format is different and I’m hoping there is an easy tweak to make it work.
I’m using the
F j, Yformat for my date. Is it possible to sort by date with this format?December 28, 2022 at 7:36 pm #2476367Fernando Customer Support
Hi John,
Can you try this?:
add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) { // apply filter if loop has class: my-class-name if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'in-date-order' ) !== false ) { // merge meta_key my-custom-field into query return array_merge( $query_args, array( 'meta_key' => 'date_time', 'meta_type' => 'DATE', 'orderby' => 'meta_value', 'order' => 'DESC', ) ); } return $query_args; }, 10, 2 );December 28, 2022 at 10:12 pm #2476431John
Fernando,
Thank you, unfortunately I had tried that and it doesn’t work (I tried again after your response, just to confirm). However, throughout the course of my research, I have come to the conclusion that it would be better practice to store the date in the database into a database-friendly format rather than a human-friendly format, and then convert the format on the fly for display in the front-end. If you disagree let me know, but unless I hear otherwise I will explore this option instead.
My goal is now to save the date in the default format, and convert it on the fly on the front-end to whichever format is selected into the WordPress admin settings. That way my users can change that format from the settings without having to touch the code. If you have an idea of how to achieve that, I would appreciate it greatly, but I feel like this is now steering away from OP’s question so I may create a new thread.
December 28, 2022 at 10:21 pm #2476438Fernando Customer Support
Yes, this is possible. And, yes, can you open a new topic regarding this?
December 28, 2022 at 10:22 pm #2476440John
Absolutely 🙂 thank you
December 28, 2022 at 10:43 pm #2476454Fernando Customer Support
You’re welcome, John! 🙂
January 16, 2023 at 2:29 pm #2498089Gary
where can I get a list of meta key and type to target with this filter?
January 16, 2023 at 5:38 pm #2498223Fernando Customer Support
Hi Gary,
The meta key targetted here in this thread is a custom meta field created by the Customer. As it’s a custom field created through ACF, there’s no list for that. The meta key name depends on what you set it to be.
If you’re using a plugin for your additional fields, and you didn’t create these fields, it would be best to reach out to the plugin’s support for a list of their custom post meta keys.
January 16, 2023 at 7:18 pm #2498280Gary
got it… thanks!
-
AuthorPosts
- You must be logged in to reply to this topic.