Slider Revolution WP
09.06.2016
Let’s say you have two main categories for your posts:
And posts from these main categories also have one or more of the same additional categories.
Now lets say you only want to include posts that have “Europe” as a category, but also only want to include “Europe” posts with either “Flights” or “Hotels”, and NOT “Dining”.
So the logic would be:
(Europe AND Flights) OR (Europe AND Hotels)
This can be accomplished by hooking into one of the plugin’s PHP filters.
In the following code, the number “121” is the Category ID for “Europe”. Adjust this number accordingly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
add_filter('essgrid_modify_posts', 'eg_modify_post', 10, 2); function eg_modify_post($posts, $grid_id){ $filterPosts = array(); foreach($posts as $post) { foreach($post['post_category'] as $cat) { // where "121" is the Category ID or "Europe" if($cat === 121) { $filterPosts[] = $post; break; } } } return $filterPosts; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
add_filter('essgrid_modify_posts', 'eg_modify_post', 10, 2); function eg_modify_post($posts, $grid_id) { $filterPosts = array(); foreach($posts as $post) { $cats = get_the_terms($post['ID'], 'product_cat'); foreach($cats as $cat) { // where "121" is the Category ID or "Europe" if($cat->term_id === 121) { $filterPosts[] = $post; break; } } } return $filterPosts; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
add_filter('essgrid_modify_posts', 'eg_modify_post', 10, 2); function eg_modify_post($posts, $grid_id){ $filterPosts = array(); foreach($posts as $post) { $cats = $post['post_category']; foreach($cats as $cat) { // parent-category ID is "25" or "26" if($cat === 25 || $cat === 26) { $subs = get_categories(array('child_of' => $cat)); foreach($subs as $sub) { $id = $sub -> cat_ID; if(in_array($id, $cats)) { // sub-category ID is "27" or "28" if($id === 27 || $id === 28) { $filterPosts[] = $post; } } } } } } return $filterPosts; } |