Elementor custom query filter with WPEM functions #1394
-
I need to write an Elementor custom query filter for a Loop Grid that displays WPEM events. How do I do this? Since there's no developer documentation for WPEM, I'm forced to ask for help here. I started with this, but I realize I probably shouldn't throw away the existing query and replace it with a new one (mainly because I can't predict pagination on the front end). How can I use WPEM's API defined in functions.php to modify the existing query? /* Query the WP Event Manager plugin for all future events, sorted by date/time ascending
* This snippet follows the Elementor guidance at
* https://developers.elementor.com/docs/hooks/custom-query-filter/
*/
function wpem_query_by_event_date( $query ) {
$opt = get_option( 'event_manager_hide_expired' );
update_option( 'event_manager_hide_expired', true );
$args = array(
// 'offset' => 0,
// 'posts_per_page' => 15,
'orderby' => 'event_start_date_time',
'order' => 'ASC',
);
$wpem_query = get_event_listings( $args );
update_option( 'event_manager_hide_expired', $opt ); # Restore the option setting
return $wpem_query;
}
add_action( 'elementor/query/wpem-by-date', 'wpem_query_by_event_date' ); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I figured it out. Code for anybody curious. It depends on WPEM keeping their database schema stable, but I'm pretty sure that's a reliable assumption. /* Query the WP Event Manager plugin for all future events, sorted by date/time ascending
* This snippet follows the Elementor guidance at
* https://developers.elementor.com/docs/hooks/custom-query-filter/
*/
function wpem_query_by_event_date_future( $query ) {
# DS: I wrote this by looking at WPEM's source code at
# https://github.com/wpeventmanager/wp-event-manager/
# because they don't have any published developer documentation (yet).
// Get current meta Query. If not exists, init as empty.
$meta_query = $query->get( 'meta_query' );
if ( ! $meta_query ) {
$meta_query = [];
}
// Append
$meta_query[] = array(
'key' => '_event_start_date',
'value' => date('Y-m-d H:i:s'), // current datetime
'compare' => '>=',
'type' => 'DATETIME',
);
$query->set( 'meta_query', $meta_query );
$query->set( 'meta_key', '_event_start_date' ); // NB: This is a field in WPEM's schema!
$query->set( 'orderby', 'meta_value' );
$query->set( 'order', 'ASC' );
return $query;
}
add_action( 'elementor/query/wpem-future-by-date', 'wpem_query_by_event_date_future' ); |
Beta Was this translation helpful? Give feedback.
I figured it out. Code for anybody curious. It depends on WPEM keeping their database schema stable, but I'm pretty sure that's a reliable assumption.