-
cleanpagedom
Hi there
I’m trying to follow this tutorial (https://docs.generateblocks.com/article/order-query-loop-by-custom-field/) in order to display a custom post type “Events” in date order in a query loop – based on the date selected in ACF Pro date/time picker. So far, I’m having no joy.
The custom post type is called “Events”; the query loop is using this post type
The custom field is called “_event_date_time”
I’ve added the class “order-by-date” to the grid item immediately inside the query loop.
Then added this code to functions.php in my child theme:
add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) { // apply filter if loop has class: order-by-date if ( ! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'order-by-date' ) !== false ) { $query_args = array_merge( $query_args, array( 'meta_key' => '_event_date_time', 'meta_type' => 'DATETIME', 'orderby' => 'meta_value', 'order' => 'ASC', )); } return $query_args; }, 10, 2 );Please can you let me know where I am going wrong?
Thanks
Dom -
David
I’m having exactly the same issue with a CPT of Event and am using exactly the same php as cleanpagedom is. The only variation is the className – mine is order-by-date-time.
My ACF field is a Date Time Picker returning a date-time format. ACF docs specify the return meta-type is DATETIME.
On my issue, it’s further complicated as I have a custom archive page with two query loops – the first to feature the closest upcoming event, the second to list other events with an offset of -1. When I assign the same CSS class to the second block, the return is including the offset post. The GB php query args do not include the ‘post_per_page’ offset. Is the GB php filter totally replacing the query args?
Sample query php from ACF docs:
<?php $posts = get_posts( array( 'posts_per_page' => -1, 'post_type' => 'event', 'order' => 'ASC', 'orderby' => 'meta_value', 'meta_key' => 'start_date', 'meta_type' => 'DATETIME', )); if( $posts ) { foreach( $posts as $post ) { // Do something. } }Really appreciate the support y’all provide – super helpful.
David -
Hi cleanpagedom,
The code you are using seems correct.
Can you link us to the page where the query loop is being added?
Hi David,
Can you open a new topic for your question to avoid complexity?
Thanks! -
David
Hi Ying –
I started to do as you requested and was setting the site up for you. I have some kind of magic working in my favor. I modified the php and named the CSS class orderby-dtime. Plugged the changes in, dropped them into the code, committed, updated the CSS class names on the grids, and it all works correctly, including the offset. BTW I tested the query args with both ASC and DESC orderby statement – no issue.I hope cleanpagedom got his working. Have a fantastic day,
David -
Hi David,
Very glad to hear that 🙂
Good job! -
David
Quick side note. Looks like this PHP can be extended with embedded elseif statements to have a conditional set of query args returned. I think I get it…
-
cleanpagedom
-
Fernando
Hi Dom,
Your code seems to be right.
Can you try creating a Hook Element with this code hooked to
generate_after_header?:<?php var_dump(get_post_meta(get_the_ID(), '_event_date_time')); ?>Set it to display on the Events – All Events.
Make sure the PHP is enabled in your Hook Element settings.
What this code will do is output the value of
_event_date_timeafter the header. Let’s first check if it will output anything. -
cleanpagedom
Thanks Fernando. It does something!
I get: “array(0) { }” at the top of the page.
https://staging-inspirewomenoldham-co-uk.stackstaging.com/events/
What now? 😀
-
David
Hi there,
where did you get the
_event_date_timemeta key name from ?
is it possible to confirm that is the correct field you need with the plugin author ? -
cleanpagedom
Hi David
I created it in ACF Pro.
Is that what the meta key is, or is that the mistake I’ve made?
Screenshot of the ACF field group here: https://ibb.co/qsnJFCz
-
David
can you test this in the test hook instead:
<?php the_field('_event_date_time'); ?>Does that turn more than array ?
-
cleanpagedom
That doesn’t show me anything.
The _event_date_time is what’s displayed beneath the heading of each event in the query loop, so I’m guessing it’s working in some capacity.
I just tried changing the name of the field to _inspire_event_date_time just in case it was conflicting, but that hasn’t done anything either.
-
Fernando
That’s odd. Can you try creating a new field Group with a new field for this? Then, try retrieving this new field through the Hook Element.
Let’s see if anything will appear.
Make sure to add any arbitrary value to this new field for the Events.
-
cleanpagedom
Hi Fernando
I created a “test_date_field” custom field and added some info to it on one of the events posts. It’s displaying propery in the query loop – beneath the first event “Test Event Order”.
I then added this to the var_dump hook.
I’m still getting “array(0) { }” showing up.
https://staging-inspirewomenoldham-co-uk.stackstaging.com/events/
Thanks
Dom -
David
Ok, check the code in your original topic:
https://generate.support/topic/order-cpt-in-query-loop-by-custom-field/#post-25201
In the first If condition you will see:
$attributes['className'], 'order-by-date'The
order-by-dateis the CSS Class that has to be added to the Query Loop grid block for the code to apply to it.Can you make sure the classname in your code and in your grid blocks Advanced > Additional CSS Classes field matches.
- You must be logged in to reply to this topic.