Here is an easy way to check if a user has a role. Note that I used your way of getting the WP_User, but there are others:
$user = new WP_User( $item->post_author );
// $user = get_userdata( get_current_user_id()); // another way to get logged in user
if ( !user_can($user->ID,'directory_4') ) {
// show items, user does not have directory_4 role
}
Related Posts:
- What do ++ and *+ mean?
- How to fix the admin menu margin-top bug in WordPress 5.5?
- Cleaning up WordPress to improve performance?
- How to add date range in woocommerce with code
- How can I modify the permalink via a filter?
- How build a custom login/register form with error handling?
- How can merge two arrays values in one array and save in database
- WordPress remove EXIF Data from specific Thumb
- Network installation giving errors when viewing or adding a user
- Create a post variable processing page
- The best way to customize “nav-menu-template.php” to add if the ‘link_before’ is “checkbox”
- List all categories but exclude current post category on single post page
- How Do I Add User Custom Field to REST API Response?
- Bare minimum to include in PHP file to use WP functions? [duplicate]
- Woocommerce, recognize the loop of related products
- Should I store my Facebook access tokens?
- Using FirePHP with WordPress
- How to return output of the new wp gallery block for automatically Link images To Media file
- Can an array be used as a meta_query value?
- WordPress different language footer text
- Does wp-cron runs all tasks scheduled at same time together or one after other?
- Filter get_the_title to remove certain characters?
- How do I target the child theme with get_bloginfo();?
- If Meta Checkbox is checked, do something?
- Output 2 items within the Loop
- Why does running get_the_excerpt() when generating JSON file take 28 seconds vs. 599 milliseconds without it?
- Can I use HTTP POSTs? Is there a better alternative?
- Displaying a button on each post
- Will simple function names in a class structure conflict with other plugins?
- Store records for a table in wordpress
- WooCommerce Related Products by Attribute
- Image as Sales Badge
- Getting a specific post values to another div or modal
- How can i upload images in an admin page?
- How to Arrange PHP Files to Allow for Multiple Types of Pages?
- WP grandchild categories in nested ul li
- Modify HTML structure of fields in woocommerce checkout form
- Retrieve $_POST data submitted from external URL in WordPress(NOT API)
- Limit Widgets to Sidebar ID’s
- How to improve my WordPress filter to include empty custom fields
- Update results, Before deleting the related category [closed]
- Trouble checking if custom woocommerce checkout field is empty or not
- Add / Update Custom Fields After Select Pictures in Media Window
- wordpress archive a user
- How to debug this search & replace strings snippet?
- How to Return in WordPress Function php file?
- Limit Taxonomy Output in Conditional Statement
- wp_redirect only works on main site and not on other sites
- Can’t retrieve element with WordPress default @fetch_rss();
- I have a snippet to redirect all users to a maintenance page. How do I exclude users with admin role?
- How to filter custom taxonomy term name, slug, and description?
- Rewrite with pagination /foo/page/2/ to posts of a given category, page 2
- Remove empty terms from array, sort alphabetically, update back to repeating field
- Call WC_Product get_price()?
- Is this a correct usage of ob_start() in my WordPress project?
- How do I get my Javascript scripts working?
- Can not add admin notices from the edit_user_profile_update hook (notices not being displayed)?
- How to remove the space before the ellipsis in excerpt?
- Website completely messes up when logging out
- Searching Posts Programmatically in a WordPress Plugin
- Unique PHP on each Page
- How do I include an external PHP file in a subdir WordPress install?
- page 1 is not paged
- Page Options Saving But Not Updating In Backend
- Drupal is going Symfony. Would WordPress benefit from doing the same? [closed]
- no_rest_route error on custom routes
- WordPress Settings API unable to save settings
- How to redirect non logged in mobile users to page on same site?
- HTML Form Question
- Decode and Decrypt Azure B2C OpenID Authorization Token, Use Response in API Call (Example Token Within)
- How do I use an iFrame for my header in WordPress twenty seventeen?
- How to put the day of the function get_the_modified_date (‘l’, $post_id’) with the first capital letter?
- Use WPQuery to match to specific repeater row in post
- Adding variable in WordPress shortcode
- Restrict wordpress access to logged users only
- Execute Shortcode in Textfile
- Featured images duplicating and replacing next image
- Set a cookie inside the 404.php theme page
- How best to check if a user is from China and hide content?
- Bring Font Awesome icons inside menu A tags
- Use WP Cron to Clear a Page Cache?
- How to use something like meta_query but for fields?
- Local WordPress from Git repo, where to set document root?
- Sort WooCommerce data with WP-CRON?
- Array sorting assistance
- Apply filters when loading post via ajax
- Relative post date in recent posts list
- Shortcode At top of Page – PHP 5.5 vs 5.4 – ob_start();
- How to fetch results from database
- PHP version or config prohibiting script from working properly
- Include a php file that is above WP installation hierarchically?
- Submenu opened problem
- Does wp_query and query_posts affect website performance? [duplicate]
- Facebook share button only share the_permalink(), without thumbnail, tilte, excerpt, etc
- ACF | WooCommerce | Theme Development | How to include a /template-part/ that makes use of ACF’s on a custom WooCommerce homepage?
- Woocommerce – Shipping tax class based on cart items not using the highest tax available
- ACF repeater image in video poster with jquery
- append special url end of my website urls
- Putting form result in my database
- SQL – How to echo out inside of where statement