Saltar navegación

[Resuelto] custom query

This support ticket is created 4 years, 11 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 -

Zona horaria del colaborador: America/Jamaica (GMT-05:00)

Este tema contiene 3 respuestas, tiene 2 mensajes.

Última actualización por Shane 4 years, 11 months ago.

Asistido por: Shane.

Autor
Mensajes
#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?
enlace oculto

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

#2113581

Shane
Colaborador

Idiomas: Inglés (English )

Zona horaria: 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
Colaborador

Idiomas: Inglés (English )

Zona horaria: 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