Home › Forums › Calendar Products › Events Calendar PRO › Exclude category from List/Photo view messes up List Widget query
- This topic has 7 replies, 3 voices, and was last updated 7 years, 4 months ago by Brook.
-
AuthorPosts
-
December 19, 2016 at 12:37 pm #1207876leviticusParticipant
Based on other discussions in the forum, I used the following code to exclude “private” and “office” from the main List and Photo views. It worked great, except when I use the Events List Widget on a single page, the widget shows all upcoming events rather than just the categories I selected for that specific widget.
Here’s the code I have:
add_action( 'pre_get_posts', 'tribe_exclude_events_category_month_list' ); function tribe_exclude_events_category_month_list( $query ) { if ( isset( $query->query_vars['eventDisplay'] ) && ! is_singular( 'tribe_events' ) ) { if ( $query->query_vars['eventDisplay'] == 'list' && ! is_tax( Tribe__Events__Main::TAXONOMY ) || $query->query_vars['eventDisplay'] == 'photo' && $query->query_vars['post_type'] == Tribe__Events__Main::POSTTYPE && ! is_tax( Tribe__Events__Main::TAXONOMY ) && empty( $query->query_vars['suppress_filters'] ) ) { $query->set( 'tax_query', array( array( 'taxonomy' => Tribe__Events__Main::TAXONOMY, 'field' => 'slug', 'terms' => array( 'private', 'office' ), 'operator' => 'NOT IN' ) ) ); } } return $query; }
Is there a way to limit this function not to run on is_single() pages? I tried modifying it to:
if ( isset( $query->query_vars['eventDisplay'] ) && ! is_singular( 'tribe_events' ) && ! is_single() ) {
But it didn’t do the trick.December 21, 2016 at 10:00 am #1209021BrookParticipantHowdy Leviticus,
It would be my pleasure to assist with this.
You should be able accomplish this by adding this to the top of your function:
if ( is_single() && tribe_is_event() ) return $wp_query;
If you want to stop it from running on all singles, and not just tribe events, you could remove theĀ && tribe_is_event().
Did that do the trick?
Cheers!
– Brook
December 22, 2016 at 1:27 pm #1209781leviticusParticipantHmm… I tried that line in couple different spots in the function, but it is not working on the List Widget on a single page.
Did you mean like this?
add_action( 'pre_get_posts', 'tribe_exclude_events_category_month_list' ); function tribe_exclude_events_category_month_list( $query ) { if ( is_single() && tribe_is_event() ) return $wp_query; if ( isset( $query->query_vars['eventDisplay'] ) && ! is_singular( 'tribe_events' ) ) { if ( $query->query_vars['eventDisplay'] == 'list' && ! is_tax( Tribe__Events__Main::TAXONOMY ) || $query->query_vars['eventDisplay'] == 'photo' && $query->query_vars['post_type'] == Tribe__Events__Main::POSTTYPE && ! is_tax( Tribe__Events__Main::TAXONOMY ) && empty( $query->query_vars['suppress_filters'] ) ) { $query->set( 'tax_query', array( array( 'taxonomy' => Tribe__Events__Main::TAXONOMY, 'field' => 'slug', 'terms' => array( 'private', 'office' ), 'operator' => 'NOT IN' ) ) ); } } return $query; }
December 22, 2016 at 1:29 pm #1209783leviticusParticipantThis reply is private.
December 26, 2016 at 3:54 pm #1210619BrookParticipantThanks for sharing links to the pages. That helped clarify what you want. Before I was thinking you had a widget in the sidebar on single events pages, but you are running this as a shortcode or widget on a regular WordPress page, not a single event.
In that case I would replace:
if ( is_single() && tribe_is_event() ) return $wp_query;
With:
if ( is_page() || is_single() ) return $query;
As an aside, in WordPress it is common coding practice to put return statementsĀ at the top of a function inside of some exit conditions. So instead of doing:
function name( $output ) { if ( something() ) { // insert code here } return $output; }
WordPress devs typically do:
function name( $output ) { if ( ! something() ) return $output; // insert code here return $output; }
There is no functional difference between these. I see your original code is using nested if statements, per the first example. If you prefer that method feel free to alter my code. It won’t change how things behave.
Did that work?
Cheers!
– Brook
December 28, 2016 at 8:29 am #1211035leviticusParticipantI think that worked! Thank you for your help.
January 4, 2017 at 1:38 pm #1213438BrookParticipantYou’re very welcome. Cheers!
– Brook
-
AuthorPosts
- The topic ‘Exclude category from List/Photo view messes up List Widget query’ is closed to new replies.