Support

Home Forums Event Espresso Premium Password-protected events are appearing when we use an event shortcode

Password-protected events are appearing when we use an event shortcode

Posted: May 28, 2021 at 2:09 am

Viewing 6 reply threads


codingforsail

May 28, 2021 at 2:09 am

Hi,

A while ago we implemented the code in the following ticket:
https://eventespresso.com/topic/how-do-we-hide-events-that-have-sold-out/

to remove password-protected and sold out events from event lists. It has been working fine.

We are switching over to using category-specific event shortcodes instead, eg
[ESPRESSO_EVENTS category_slug=sailing show_title=false limit=5 css_class=essa_events_css]

However, the function above is not being called and password-protected events are being shown.

Do I need to use a different filter/hook?

Best wishes,

Anita


Tony

  • Support Staff

May 28, 2021 at 5:38 am

Hi Anita,

That function doesn’t remove password-protected events, it only prevents posts with a status of ‘sold_out’ from showing.

May I ask how you previously displayed the events where the password-protected events didn’t show?


codingforsail

May 30, 2021 at 11:32 am

We were previously just using the event list archive.
There is a line in the code I pasted that alters the query to select events with a password of ”
$SQL .= "AND {$wpdb->prefix}posts.post_password = '' ";


codingforsail

May 31, 2021 at 3:36 am

I remember adding that line of code, but don’t know where to add similar for events produced using the shortcode.


Tony

  • Support Staff

June 1, 2021 at 5:34 am

My apologies, that’s me being completely blind to half of the code!

So the code used in that snippet should just apply to both the archive and instances of the ESPRESSO_EVENTS shortcode by default. It isn’t checking if you are within the main query so applies any time WP_Query is used (the shortcode uses it) to pull espresso_events posts.

I just threw your snippet into a custom functions plugin to confirm if this worked and didn’t run into any problems:

https://monosnap.com/file/GdEyVGL8qxpGYuNGi2m5donqc6PxxZ

Thats just using:

add_filter( 'posts_where', 'tw_ee_event_list_exclude_sold_out_and_pwprotected_events_where', 15, 2 );
function tw_ee_event_list_exclude_sold_out_and_pwprotected_events_where( $SQL, WP_Query $wp_query ) 
{
    if ( 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;
        $SQL .= "AND {$wpdb->prefix}posts.post_password = '' ";
        $SQL .= "AND {$wpdb->prefix}posts.post_status != 'sold_out' ";
    }
    return $SQL;
}

You could try adding a var_dump() call before the if statement and output something there to confirm if the code is running but failing on one of the conditionals.


codingforsail

June 1, 2021 at 10:46 am

No worries. I look at things but don’t see them sometimes too.

In this case, the code is working right, it’s just that some of the events which should be password-protected don’t have a password!


Tony

  • Support Staff

June 1, 2021 at 1:58 pm

Ah, yeah, that’ll also do it 🙂

Glad you found the cause.

Viewing 6 reply threads

The support post ‘Password-protected events are appearing when we use an event shortcode’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso