Skip to content

Instantly share code, notes, and snippets.

@Camwyn
Forked from cliffordp/functions.php
Last active June 10, 2022 20:48
Show Gist options
  • Save Camwyn/15e1936639a5ea7b1575b35f252ebd40 to your computer and use it in GitHub Desktop.
Save Camwyn/15e1936639a5ea7b1575b35f252ebd40 to your computer and use it in GitHub Desktop.
Limit Essential Grid's WP_Query to only include Featured Events that have a featured image
<?php
/**
* Limit Essential Grid's WP_Query to only include Featured Events that have a featured image
*
* @link https://theeventscalendar.com/knowledgebase/featured-events/
*
* Based on https://gist.github.com/cliffordp/a6aad3c60f469970b1ecb209d85ec755
* Similar code for Slider Revolution: https://gist.github.com/Camwyn/ce34677cafe52d46c12c11c8b9119371
* Same Essential Grid code except without requirement for being a Featured Event (works for more than just The Events Calendar): https://gist.github.com/Camwyn/cc191bc1c95581c38f7ed5f8ba61da11
*
* essgrid_get_posts filter is from /wp-content/plugins/essential-grid/includes/base.class.php
*
* Tested working with version 6.5.24
*
* Filter hook only applies to Essential Grid > Source > Source Based on Posts, Pages, Custom Posts
*
* @return array
*/
function essgrid_post_based_featured_events_w_featured_image( $query, $grid_id ) {
/* YOU MUST CHANGE THESE TO YOUR OWN SLIDER REVOLUTION SLIDER IDs!!! */
$grid_ids_to_affect = array( 1, 2, 14 ); // include IDs of grids to affect
// if this grid is not one to affect, do no filtering
if ( ! in_array( $grid_id, $grid_ids_to_affect ) ) {
return $query;
}
// get the existing meta_query so we aren't wiping that out
if ( ! empty( $query['meta_query'] ) ) {
$meta_query = (array) $query[ 'meta_query' ];
} else {
$meta_query = [];
}
// do the filtering...
// has a Featured Image
$meta_query[] = array(
'compare' => 'BETWEEN',
'key' => '_thumbnail_id',
'value' => [ 1, PHP_INT_MAX ],
);
// Restrict to events(posts) that have are featured events.
if ( class_exists( 'Tribe__Events__Main' )
&& class_exists( 'Tribe__Events__Featured_Events' )
&& in_array( Tribe__Events__Main::POSTTYPE, (array) $query['post_type'] )
) {
$meta_query[] = [
'compare' => 'EXISTS',
'key' => Tribe__Events__Featured_Events::FEATURED_EVENT_KEY,
];
}
$query['meta_query'] = $meta_query;
return $query;
}
add_filter( 'essgrid_get_posts', 'essgrid_post_based_featured_events_w_featured_image', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment