-
-
Save Camwyn/d7128fb83074d75e1562adac2735a834 to your computer and use it in GitHub Desktop.
Limit Slider Revolution's WP_Query to only include posts with featured images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Limit Slider Revolution's WP_Query to only include posts with featured images | |
* | |
* Based on: https://gist.github.com/cliffordp/9f0d7cc5e86b2a721cd646d953bb1261 | |
* Similar code for Essential Grid: https://gist.github.com/Camwyn/cc191bc1c95581c38f7ed5f8ba61da11 | |
* Same Slider Revolution code except with additional requirement to be a Featured Event from The Events Calendar: https://gist.github.com/Camwyn/ce34677cafe52d46c12c11c8b9119371 | |
* | |
* Tested working with version 6.5.24 | |
* | |
* `revslider_get_posts` filter is from plugins/revslider/includes/slider.class.php as of version 6.5.24 | |
* | |
* 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) | |
* There are others in plugins/revslider/includes/slider.class.php for other queries. | |
* | |
* @return array | |
*/ | |
function revslider_post_based_require_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 = []; | |
} | |
// do the filtering... | |
// Restrict to events(posts) that have a featured image. | |
$meta_query[] = [ | |
'compare' => 'BETWEEN', | |
'key' => '_thumbnail_id', | |
'value' => [ 1, PHP_INT_MAX ], | |
]; | |
$query['meta_query'] = $meta_query; | |
return $query; | |
} | |
add_filter( 'revslider_get_posts', 'revslider_post_based_require_featured_image', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: the original of this used
'compare' => 'EXISTS'
which would fail often as a missing image can be saved in the database as a NULL value - thus it "exists".Instead we test that it's a number between 1 (skipping post 0 which is typically a demo post) and
PHP_INT_MAX