Skip to content

Instantly share code, notes, and snippets.

@cliffordp
Last active September 27, 2022 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save cliffordp/30ac2152a8264ef27235b46b7d16332d to your computer and use it in GitHub Desktop.
Save cliffordp/30ac2152a8264ef27235b46b7d16332d to your computer and use it in GitHub Desktop.
Limit Slider Revolution's WP_Query to only include Featured Events that have a featured image
<?php
/**
* Limit Slider Revolution'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/30ac2152a8264ef27235b46b7d16332d
* Similar code for Essential Grid: https://gist.github.com/cliffordp/a6aad3c60f469970b1ecb209d85ec755
* Same Slider Revolution code except without requirement for being a Featured Event (works for more than just The Events Calendar): https://gist.github.com/cliffordp/9f0d7cc5e86b2a721cd646d953bb1261
*
* revslider_get_posts filter is from /wp-content/plugins/revslider/includes/framework/functions-wordpress.class.php as of version 4.5.0
*
* Filter hook only applies to Slider Revolution > Post-Based Slider > Fetch Posts By Categories & Tags
* or Slider Revolution > Post-Based Slider > Specific Posts > Specific Posts List (CSV of Post IDs)
*
* @return array
*/
function revslider_post_based_featured_events_w_featured_image( $query, $slider_id ) {
/*
* YOU MUST CHANGE THESE
* TO YOUR OWN SLIDER REVOLUTION
* SLIDER IDs
* !!!
*/
$slider_ids_to_affect = array( 2, 20, 34 ); // include IDs of sliders to affect
// if this slider is not one to affect, do no filtering
if ( ! in_array( $slider_id, $slider_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( 'revslider_get_posts', 'revslider_post_based_featured_events_w_featured_image', 10, 2 );
@blupointmedia
Copy link

blupointmedia commented Jul 28, 2021

Based on this and my integration this statement will never be true. Tribe__Events__Main::POSTTYPE === $query['post_type']
$query['post_type'] is an array and therefore needs to be checked like this $query['post_type'][0] -- adding this for others for future use

@Camwyn
Copy link

Camwyn commented Jun 8, 2022

@blupointmedia : A safer bet would be to change Tribe__Events__Main::POSTTYPE === $query['post_type']
to in_array( Tribe__Events__Main::POSTTYPE, (array) $query['post_type'] );

As post_type can be a string OR an array - then you are covered either way 😉

@john29800
Copy link

G'day guys,
Does it mean that Revolution slider can render images from custom meta fields by adding this to the function.php ?
Cause a client of mine has a listing theme and a gallery field (_gallery) made thru CMB2, he asked me for a sliders on his listings but I haven't find a way to render a gallery yet... I even tried Elementor Dynamic tags.

@cliffordp
Copy link
Author

@john29800 I'm unclear exactly what you want and don't provide support here. Plus, I'm unsure how your question relates to The Events Calendar plugin.

You might want to look into using the WordPress default [gallery ... ] shortcode -- maybe like do_shortcode( '[gallery ids="729,732,731,720"]' );

Some plugins (e.g. Jetpack) can override the default gallery shortcode, and some plugins have their own custom shortcodes or PHP functions you could insert.

Maybe helpful:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment