Created
January 3, 2018 15:54
-
-
Save joshfeck/306e3fd441c90de62c4cd0453b029a9f to your computer and use it in GitHub Desktop.
You can use this code to hide events that belong to a specific event category if the user is not logged into the website. Requires EE4
This file contains 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 | |
//* Please do NOT include the opening php tag, except of course if you're starting with a blank file | |
add_filter( 'posts_where' , 'my_filter_an_event_category', 11, 2 ); | |
function my_filter_an_event_category( $where, WP_Query $wp_query ) { | |
if ( | |
!current_user_can('read') && | |
isset( $wp_query->query_vars['post_type'] ) && | |
( $wp_query->query_vars['post_type'] == 'espresso_events' || | |
( is_array( $wp_query->query_vars['post_type'] ) && | |
in_array( 'espresso_events', $wp_query->query_vars['post_type'] ) ) ) && | |
! $wp_query->is_singular | |
) | |
{ | |
global $wpdb; | |
//exclude events which have event category id=154 | |
$filter = " AND tt.term_id IN ('154')"; | |
$where .= " AND $wpdb->posts.ID NOT IN "; | |
$where .= "(SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy = 'espresso_event_categories'".$filter.")"; | |
} | |
return $where; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment