Filtering custom posts by custom taxonomy and user role using get_posts

I’m trying to filter my custom posts in a get_posts loop by first checking if the current user has a particular role and then filtering by a custom taxonomy term. However, I can’t seem to get the posts to filter – can anyone see what I’m doing wrong in my code below?

Thanks,

Osu

/* ****************************************************************** */
/* !FILTER POSTS BY ROLE */
/* ****************************************************************** */

// Set args
$event_args = array(
‘posts_per_page’ => -1,
‘post_parent’ => 0, // Only get top-level events
‘orderby’ => ‘post_date’,
‘order’ => ‘DESC’,
‘post_type’ => ‘dbevents’,
‘post_status’ => ‘publish’
);

// Set tax term to nothing so shows everything
$tax_term = ”;

// Get current user role and exclude admin and editor roles
$current_user = wp_get_current_user();
if( is_admin() ) {

// nada

} elseif( in_array( $current_user->roles, ‘editor’ ) ) {

// nada

} elseif( in_array( $current_user->roles, ‘db_private’ ) ) {

// Only show private events
$event_args[‘tax_query’] = array(
array(
‘taxonomy’ => ‘dbeventtype’,
‘field’ => ‘slug’,
‘terms’ => ‘private’, // only shows posts with custom taxonomy term ‘private’
‘include_children’ => false
),
);

} elseif( in_array( $current_user->roles, ‘db_discourse’ ) ) {

// Only show discourse events
$event_args[‘tax_query’] = array(
array(
‘taxonomy’ => ‘dbeventtype’,
‘field’ => ‘slug’,
‘terms’ => ‘discourse’, // only shows posts with custom taxonomy term ‘discourse’
‘include_children’ => false
),
);

}

/* ****************************************************************** */
/* !Start loop */
/* ****************************************************************** */

$get_event_posts = get_posts( $event_args );
if($get_event_posts) :

// The loop
foreach ( $get_event_posts as $post ) : setup_postdata( $post ); ?>
<article <?php post_class(‘event-entry’); ?> id=”post-<?php the_ID(); ?>”>
<a href=”<?php the_permalink(); ?>” title=”<?php printf( esc_attr__( ‘Permalink to %s’, ‘dbevents’ ), the_title_attribute( ‘echo=0’ ) ); ?>” rel=”bookmark” class=”animate”>
<?php osu_featured_img(‘large’, ‘featured-img-sidebar’); ?>
<span class=”details”>
<?php the_title(); ?>
<span><?php echo osuShowTaxTerms(‘dbeventcountries’, get_the_ID()); ?> – <?php the_field(‘date’); ?></span>
</span>
</a>
</article>
<?php endforeach;

// Reset loop data
wp_reset_postdata();

else : ?>

<article id=”nada”>
<p>Currently, there are no upcoming events scheduled.</p>
</article>

<?php endif; ?>

Read more here:: Filtering custom posts by custom taxonomy and user role using get_posts

Leave a Reply

Your email address will not be published. Required fields are marked *