Exclude category from List/Photo view messes up List Widget query

Home Forums Calendar Products Events Calendar PRO Exclude category from List/Photo view messes up List Widget query

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #1207876
    leviticus
    Participant

    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.

    #1209021
    Brook
    Participant

    Howdy 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

    #1209781
    leviticus
    Participant

    Hmm… 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;
    }
    
    #1209783
    leviticus
    Participant

    This reply is private.

    #1210619
    Brook
    Participant

    Thanks 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

    #1211035
    leviticus
    Participant

    I think that worked! Thank you for your help.

    #1213438
    Brook
    Participant

    You’re very welcome. Cheers!

    – Brook

Viewing 7 posts - 1 through 7 (of 7 total)
  • The topic ‘Exclude category from List/Photo view messes up List Widget query’ is closed to new replies.