-
fjvanittersum
Hi,
I have had a lot of help of you on showing custom date fields in GPP and GB. Now I am using the Query Loop Sort by Custom Field script (Example C: Date Values): https://docs.generateblocks.com/article/order-query-loop-by-custom-field/ This script defining a class is great.
I use the custom date field for event dates. I try to use GPP and GB to create a simple event calendar.
Can the order-by-priority date script (Example C) be modified in a way that the QueryLoops-block shows posts (events) of today and the future (and not the past)? And today must be dynamic: now it is September 28th, 2023, and tomorrow September 29th, 2023. This GB QueryLoops block will be an “upcoming events block”.
Thanks !
Frans
-
Fernando
Hi Frans,
Thank you for opening a new ticket.
So, ideally, all events on your site have an “event-day” meta field value, and once that day has passed, it would no longer appear on the Query? This is considering the Events will also be ordered by this “event-day” as well.
Is this correct?
-
fjvanittersum
Fully correct!
Frans -
Fernando
Try this:
1. Add
cu-date-order
to the class list of the Grid Block inside the Query Loop Block.2. Add this snippet:
add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) { // apply filter if loop has class: cu-date-order if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'cu-date-order' ) !== false ) { // merge meta_key event_start_date into query return array_merge( $query_args, array( 'meta_key' => 'meta-field-name', 'meta_type' => 'DATE', 'orderby' => 'meta_value', 'order' => 'ASC', 'meta_query' => array( array( 'key' => 'meta-field-name', 'value' => date("Y-m-d"), 'compare' => '>', 'type' => 'DATE' ) ) ) ); } return $query_args; }, 10, 2 );
Replace
meta-field-name
in the code with your meta-field name. -
fjvanittersum
Thanks! The script works fine with one issue left.
The events with today’s date are not shown. I tried to change the compare line to
'compare' => '>='
, or'compare' => '=>'
` but this does not work. Maybe another operator is needed or the value line should be modified to yesterday.Maybe, there is an solution!
Thanks!
Frans
-
fjvanittersum
Solved!
The operator in compare must be ‘>=’. The value line must be
date("Y-m-d")
, not Y-m-d H:i:sThe same script can be used for archive puroposes. Then the operator in compare must be ‘<‘.
Thanks ! I think this question is solved!
Frans
-
Fernando
You’re welcome, Frans!
-
fjvanittersum
Dear Fernando,
For some reasons the scripts do not work as expected on my production website. If I use the order-by-priority script from your docs website, it works, even with DESC sorting:
//DATE type custom field value add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) { // apply filter if loop has class: order-by-priority if ( ! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'order-by-priority' ) !== false ) { $query_args = array_merge( $query_args, array( 'meta_key' => 'datum_evenement', 'meta_type' => 'DATE', 'orderby' => 'meta_value', 'order' => 'DESC', )); } return $query_args; }, 10, 2 );
When I use the script of this thread:
add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) { // apply filter if loop has class: cu-date-order if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'cu-date-order' ) !== false ) { // merge meta_key event_start_date into query return array_merge( $query_args, array( 'meta_key' => 'datum_evenement', 'meta_type' => 'DATE', 'orderby' => 'meta_value', 'order' => 'ASC', 'meta_query' => array( array( 'key' => 'datum_evenement', 'value' => date("Y-m-d"), 'compare' => '>=', 'type' => 'DATE' ) ) ) ); } return $query_args; }, 10, 2 );
it works, but changes to the script do not make any difference any more. E.g. changing the sorting to
'order' => 'DESC',
does not change the sorting (it remains ASC); changing the compare row to'compare' => '<',
only reveals events in the future and not in the past. I tried to change the date definition to'value' => date("Y-m-d H:i:s"),
because ACF stores date + time in this format, but it does not solve the problem.Can I transform this script to a class that only selects events from the past (i.e. posts with a date in a custom date field in the past) and sorts DESC?
Thanks !
Frans
-
Fernando
Let’s do this step-by-step,
Can you remove this part of the code first:
'meta_query' => array( array( 'key' => 'datum_evenement', 'value' => date("Y-m-d"), 'compare' => '>=', 'type' => 'DATE' ) )
and replace
ASC
withDESC
?Does that work?
-
fjvanittersum
I tried: the script does not select any post. The GB Query Loops block is empty. But, … the GB navigation below the block, indicates that there are several pages (of 10 posts per page).
I changed to the
order-by-priority
class from your docs, replacedASC
byDESC
: this is OK: it sorts descending.This is the page (now
order-by-priority
withDESC
on a custom date field.Thanks !
Frans
-
Fernando
So this one works for you?: https://docs.generateblocks.com/article/order-query-loop-by-custom-field/#example-c-date-values
If so, can you try this slightly modified version of that code adding a meta-query?:
//DATE type custom field value add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) { // apply filter if loop has class: order-by-priority if ( ! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'order-by-priority' ) !== false ) { $query_args = array_merge( $query_args, array( 'meta_key' => 'priority', 'meta_type' => 'DATE', 'orderby' => 'meta_value', 'order' => 'DESC', 'meta_query' => array( array( 'key' => 'priority', 'value' => date("Y-m-d"), 'compare' => '>=', 'type' => 'DATE' ) ) )); } return $query_args; }, 10, 2 );
Make sure to change the class and the key accordingly.
-
fjvanittersum
Thanks! Today I’am very busy at work. So, it takes some time before I can test this script.
Will be continued.
Frans -
Fernando
Hope it goes well!
-
fjvanittersum
This is excellent! Works fine on my production site.
For users: there is one disadvantage, which is a consequence of the methodology: the GB navigation, at the bottom of the Query Loops block, counts the number of posts that is selected initially (in the first step) by QueryLoops. If the script, selects (in a second step) just a few posts of the initially selected, the navigation (number of pages) does not change: it remains showing the number initially selected by QueryLoops (before the script was applied).
For me, this issue is solved !
Thanks !
Frans
-
Fernando
You’re welcome Frans!
- You must be logged in to reply to this topic.