Passer la navigation

[Résolu] custom query

This support ticket is created Il y a 4 years, 11 months. 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 -

Fuseau horaire du supporter : America/Jamaica (GMT-05:00)

Ce sujet contient 3 réponses, a 2 voix.

Dernière mise à jour par Shane Il y a 4 years, 11 months.

Assisté par: Shane.

Auteur
Publications
#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?
lien caché

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

#2113581

Shane
Supporter

Les langues: Anglais (English )

Fuseau horaire: 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

Les langues: Anglais (English )

Fuseau horaire: 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