Site logo

[Resolved] Query Loop – Use ACF Date Field as Order By Parameter

Home Forums Support [Resolved] Query Loop – Use ACF Date Field as Order By Parameter

Home Forums Support Query Loop – Use ACF Date Field as Order By Parameter

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #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.

    #2405953
    David
    Staff
    Customer Support

    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.

    #2406154
    Nick

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

    #2406193
    David
    Staff
    Customer Support

    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.

    #2406309
    Nick

    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.

    #2406658
    David
    Staff
    Customer Support

    Awesome – glad to hear that!

    #2476168
    John

    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?

    #2476367
    Fernando
    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 );
    #2476431
    John

    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.

    #2476438
    Fernando
    Customer Support

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

    #2476440
    John

    Absolutely 🙂 thank you

    #2476454
    Fernando
    Customer Support

    You’re welcome, John! 🙂

    #2498089
    Gary

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

    #2498223
    Fernando
    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.

    #2498280
    Gary

    got it… thanks!

Viewing 15 posts - 1 through 15 (of 16 total)
  • You must be logged in to reply to this topic.