Skip to content

Instantly share code, notes, and snippets.

@cliffordp
Last active June 9, 2022 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cliffordp/a6aad3c60f469970b1ecb209d85ec755 to your computer and use it in GitHub Desktop.
Save cliffordp/a6aad3c60f469970b1ecb209d85ec755 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/
*
* From https://gist.github.com/cliffordp/a6aad3c60f469970b1ecb209d85ec755
* Similar code for Slider Revolution: https://gist.github.com/cliffordp/30ac2152a8264ef27235b46b7d16332d
* Same Essential Grid code except without requirement for being a Featured Event (works for more than just The Events Calendar): https://gist.github.com/cliffordp/fbbaad70b9b8748819ac73f00260ac5e
*
* essgrid_get_posts filter is from /wp-content/plugins/essential-grid/includes/base.class.php
*
* Tested working with version 2.1.0.2
*
* 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 ESSENTIAL GRID
* GRID 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 = array();
}
// do the filtering...
// has a Featured Image
$meta_query[] = array(
'key' => '_thumbnail_id',
'compare' => 'EXISTS',
);
// is a Featured Event (requires The Events Calendar version 4.4+ from January 9, 2017)
if ( class_exists( 'Tribe__Events__Main' )
&& class_exists( 'Tribe__Events__Featured_Events' )
&& Tribe__Events__Main::POSTTYPE === $query['post_type']
) {
$meta_query[] = array(
'key' => Tribe__Events__Featured_Events::FEATURED_EVENT_KEY,
'compare' => 'EXISTS',
);
}
$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