Site logo

[Resolved] Query Loop – Order by Date

Home Forums Support [Resolved] Query Loop – Order by Date

Home Forums Support Query Loop – Order by Date

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #2498102
    Royal Rangers

    Hi, I have a custom post type for Events and using ACF for a date field for the event.

    I try this code:

    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
        // apply filter if loop has class: my-order
        if (
            ! empty( $attributes['className'] ) &&
            strpos( $attributes['className'], 'my-order' ) !== false
        ) {
            // merge meta_key my-custom-field into query
            return array_merge( $query_args, array(
                'meta_key' => 'datum',
                'orderby' => 'meta_value',
                'order' => 'DESC',
            ) );
        }
    
        return $query_args;
    
    }, 10, 2 );

    I trying this https://generatepress.com/forums/topic/query-loop-use-acf-date-field-as-order-by-parameter/#post-2405953.

    Can you help me?

    #2498228
    Fernando
    Customer Support

    Hi Jan,

    Yes, we can try to help.

    Can you try this snippet instead?:

    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
        // apply filter if loop has class: my-order
        if (
            ! empty( $attributes['className'] ) &&
            strpos( $attributes['className'], 'my-order' ) !== false
        ) {
    $query_args['orderby'] = 'meta_value';
    $query_args['meta_key'] = 'datum';
    $query_args['order'] = 'DESC';
     }
    
        return $query_args;
    
    }, 10, 2 );

    Let us know how it goes.

    #2498378
    Royal Rangers

    Code replaced but doesn´t work. The date field is in d/m/Y format. What mean meta_value? I don’t have it set Order by in Query loop and I don’t see any option for ACF there.

    #2498393
    Fernando
    Customer Support

    meta_value is the value of your custom meta field datum.

    Ordering by custom meta fields isn’t available yet but it is planned for a future update.

    Can you take a screenshot of your ACF field settings for reference?

    You may use the Private Information field for this: https://docs.generatepress.com/article/using-the-premium-support-forum/#private-information

    #2498596
    Royal Rangers

    I found a bug in setting. It works now. Is possible view custom field date in Gutenberg block with ACF free?

    #2498739
    David
    Staff
    Customer Support

    Hi there,

    yes, if you add a GenerateBlocks Block, eg. a Headline block, and enable its Dynamic Data, you can set the Content source to Post Meta and in the field provided add your ACF Field name.

    #2498761
    Royal Rangers

    It works. Thanks.

    #2498850
    David
    Staff
    Customer Support

    Glad to hear that

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