How to have a conditional query loop based on user role?

I have content that is only visible to staff members assigned the ‘staff’ user role. Additionally, I have a page that displays all website content by querying different custom post types.

Posts that are intended exclusively for staff members are tagged with ‘staff’.

Is it possible to create a query loop that:

  • Displays all posts for users with the ‘staff’ role, including those tagged as ‘staff’
  • Hides posts with the ‘staff’ tag for users who do not have the ‘staff’ role?

condition the loop itself
or the posts each
or create 2 loop one staff one for others

multiple ways to approach it practice conditions
there are tons of dynamic tags for users

Would be easy, if there would be a nice loop element

Go and comment to give it some tractions
https://forum.bricksbuilder.io/t/loopable-wrapper-element-with-no-html-output/29701

1 Like

I can’t create 2 loops. I can only have one as I have a search bar and other facets targetting that element with the query loop.

I have wrote you 3 option up there
maybe try other 2

Sorry I didn’t understand what you meant by that.

I think I need something like this:

<?php

add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) {
    
    if ( $element_id !== 'dnavwl' ) {
        return $query_vars;
    }
    
    
    if ( current_user_can( 'staff' ) ) {

        return $query_vars;
    }
    

    if ( !isset( $query_vars['tax_query'] ) ) {
        $query_vars['tax_query'] = array();
    }
    
    
    if ( count( $query_vars['tax_query'] ) > 0 ) {
        $query_vars['tax_query']['relation'] = 'AND';
    }
    
    
    $query_vars['tax_query'][] = array(
        'taxonomy' => 'ld_course_tag',
        'field'    => 'slug',
        'terms'    => 'staff',
        'operator' => 'NOT IN',
    );
    

    $query_vars['tax_query'][] = array(
        'taxonomy' => 'video_tag',
        'field'    => 'slug',
        'terms'    => 'staff',
        'operator' => 'NOT IN',
    );
    

    return $query_vars;
}, 10, 3 );

It seems to work, I’ll play a lit bit more with this tomorrow.

1 Like