Site logo

Archived topic

Query Loop - Use ACF Date Field as Order By Parameter

15 replies · Started by Nick on November 8, 2022

Viewing posts 1–15 of 16

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.

Hi 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-name change this to a CSS Class name of meaning eg. in-date-order which you need to add to the Query Loop > Grid Blocks --> Advanced -> Additional CSS Class(es) field.

2. my_custom_field needs to be changed in the code to the name of your ACF Custom field.

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 );

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' => 'DATETIME',
            'orderby' => 'meta_value',
            'order' => 'DESC',
        ) );
    }

    return $query_args;

}, 10, 2 );

That includes the 'meta_type' => 'DATETIME', which should handle that type.

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.

Awesome - glad to hear that!

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, Y format for my date. Is it possible to sort by date with this format?

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 );

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.

Yes, this is possible. And, yes, can you open a new topic regarding this?

Absolutely :) thank you

You're welcome, John! :)

where can I get a list of meta key and type to target with this filter?

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.

got it... thanks!

This archived topic is closed to new replies.