Skip Navigation

[Resolved] custom query

This support ticket is created 2 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Shane 2 years, 9 months ago.

Assisted by: Shane.

Author
Posts
#2113495

Tell us what you are trying to do?
create Elementor custom query:
I have custom posts type: event
I have a custom field: event-date

this is the query I tried:

add_action( 'elementor_pro/posts/query/upcoming', function( $query ){
$query->set('post_type', 'event');
$today = Date('Y-m-d');
$query->set('meta_query', array(
'start_date' => array(
'key' => 'wpcf-event-date',
'value' => $today,
'compare' => '>=',
)
));
$query->set('orderby', array('start_date'=>'ASC'));
} );

What is the link to your site?
hidden link

as you can see this code bring 0 results
Can you help me?

#2113581

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Barak,

Thank you for getting in touch.

The issue here is that the date field is stored as a timestamp. You function is passing the date into the query as a calendar date which won't work.

What you need to do is convert that date to a timestamp using the strtotime() function.

This should allow you to convert your date variable and allow the filter to work as intended.

Please let me know if this helps.
Thanks,
Shane

#2116393

Can you help me how to edit the code?
I am not a developer

#2116601

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Barak,

Sure the correct code would be.


add_action( 'elementor_pro/posts/query/upcoming', function( $query ){
$query->set('post_type', 'event');
$today = Date('Y-m-d');
$query->set('meta_query', array(
'start_date' => array(
'key' => 'wpcf-event-date',
'value' => strtotime($today),
'compare' => '>=',
)
));
$query->set('orderby', array('start_date'=>'ASC'));
} );

Please let me know if this helps.
Thanks,
Shane

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.