jeudi 30 août 2018

Execute function if user logged in - Wordpress

First time I post here ! Hope my question will be relevant.

I defined a function to exclude a category in a calendar which works well when I execute it like this:

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'] == 'month' && $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( 'reunions'),
                'operator' => 'NOT IN'
            )
        ) );
    }

}

return $query;} add_action( 'pre_get_posts', 'tribe_exclude_events_category_month_list' );

But what I want is to execute it if only if user is logged in. So I did this :

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'] == 'month' && $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( 'reunions'),
                'operator' => 'NOT IN'
            )
        ) );
    }

}

return $query; } 

function exclude_category_when_logged_in() {
if ( is_user_logged_in() ) {
  add_action( 'pre_get_posts', 'tribe_exclude_events_category_month_list' );
}} add_action( 'init', 'exclude_category_when_logged_in' );

But this doesn't seem to work.

Could you help please ? Thanks a lot Lucas

Aucun commentaire:

Enregistrer un commentaire