Archived topic
Query Loop - Order by Date
7 replies · Started by Royal Rangers on January 16, 2023
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?
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.
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.
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
I found a bug in setting. It works now. Is possible view custom field date in Gutenberg block with ACF free?
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.
It works. Thanks.
Glad to hear that