Passed events appearing as "Upcoming" — but only for non-admins

Home Forums Calendar Products Events Calendar PRO Passed events appearing as "Upcoming" — but only for non-admins

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1218784
    Sean Sullivan
    Participant

    Hello,

    Previous events are showing as “upcoming” on our website — but only some events, and only for some users.

    The calendar events are only available to registered members of the site.

    We’re using the [tribe_events_list] shortcode to display upcoming events on our home page, and users can also access the events on the /events page.

    When logged in as an admin, everything works great. No problems.

    But ‘subscriber’ level users are seeing old events listed as upcoming in both the widget *and* the /events page.

    I can’t find any pattern in the old events that are appearing. :/

    #1218789
    Sean Sullivan
    Participant

    This reply is private.

    #1218792
    Sean Sullivan
    Participant

    This reply is private.

    #1218798
    Sean Sullivan
    Participant

    This reply is private.

    #1218802
    Sean Sullivan
    Participant

    Looks like I’ve found the issue. Removing this code from my functions.php file did the trick:

    add_action( 'pre_get_posts', 'my_pre_get_posts' );
    
    function my_pre_get_posts( $query ) {
    
    	// Bail if current user can restrict content or edit other users' posts.
    	if ( current_user_can( 'restrict_content' ) || current_user_can( 'edit_others_posts' ) )
    		return;
    
    	if ( is_user_logged_in() ) {
    
    		$user = new WP_User( get_current_user_id() );
    
    		$meta_query = array(
    			'relation' => 'OR',
    			array(
    				'key'     => '_members_access_role',
    				'compare' => 'NOT EXISTS',
    			),
    			array(
    				'key'     => '_members_access_role',
    				'value'   => $user->roles[0],
    				'compare' => '='
    			)
    		);
    	} else {
    
    		$meta_query = array(
    			array(
    				'key'     => '_members_access_role',
    				'compare' => 'NOT EXISTS'
    			)
    		);
    	}
    
    	$query->set( 'meta_query', $meta_query );
    }

    Any idea why the conflict is there?

    #1218804
    Sean Sullivan
    Participant

    (Because I need that code to prevent non-registered users from viewing much of the members-only content).

    #1219281
    Brook
    Participant

    Howdy Sean,

    Thank you for so thoroughly detailing the issue, and sharing your code. That helps cut right to the problem. I would love to help you from here.

    Looking at your code, I am guessing the issue is that you are setting the meta_query variable, rather than updating it. This would override the current meta_query with your new details, removing anything other code has set.

    What if, at the start of your code you ran:

    $meta_query = $query->get( 'meta_query' );

    And then inside of your if ( is_user_logged_in() ) {} else {} condition you appended the details to the existing query, as new array keys, rather than overriding it. Keep in mind you will need to wrap your relation query inside an array of its own, that way it does not set the entire query to an OR relation.

    This might prove helpful:

    https://codex.wordpress.org/Class_Reference/WP_Meta_Query

    Does that all make sense? Will that work for you? Please let me know.

    Cheers!

    – Brook

    #1230664
    Support Droid
    Keymaster

    Hey there! This thread has been pretty quiet for the last three weeks, so we’re going to go ahead and close it to avoid confusion with other topics. If you’re still looking for help with this, please do open a new thread, reference this one and we’d be more than happy to continue the conversation over there.

    Thanks so much!
    The Events Calendar Support Team

Viewing 8 posts - 1 through 8 (of 8 total)
  • The topic ‘Passed events appearing as "Upcoming" — but only for non-admins’ is closed to new replies.