Get All user post that have same taxonomy or same group

It’s a very common requirement nowadays to want to displayall post of that user that have same group or same term id  (or other post types) underneath your content. It engages readers, provides them with more related material to read, and effectively makes them spend more time on your site, further improving the probability to convert. Related posts also come with added SEO benefits, though minor, as they provide internal links to more of your content (and you probably shouldn’t get obsessed over it).

How to programmatically get same group or same taxonomy WordPress posts, easily.

Sometimes, when managing large number of users, you might need to group them in different categories, depending on the type of users and the way the interact with your site. Being able to assign different groups to your users can be beneficial for a better organization of your WordPress users list.

The Users Insights user groups feature allows you to easily create custom user groups and assign one or more groups to each WordPress user. After creating and assigning groups to users, you can then view the groups assigned to each user in the Users Insights users table. You can also filter your users by the group that they belong to.

webtechsource defines in simple way. suppose one user have role editor that want to see same group post in backend. simple code is below:

add_action(‘pre_get_posts’, ‘hide_posts_to_editors’);

function hide_posts_to_editors( $query ) {
if( current_user_can(‘editor’)) {
$self = wp_get_current_user();

$user_groups = wp_get_object_terms($self->ID, ‘user-group’, array(‘fields’ => ‘all_with_object_id’));
print_r($user_groups);
foreach($user_groups as $user_gro)
{
$term_id [] = $user_gro->term_id;
}

$taxonomy = ‘user-group’;
$same_group_users_id = get_objects_in_term($term_id, $taxonomy );

$query->set( ‘author’, implode(‘,’, $same_group_users_id) );

}

change user role editor to according to your requirement.

change taxonomy that you want to get data above code taxonomy is user-group

if you need more help comment here we will provide you more better solutions.

I, Dayanand Saini,the founder and chief editor of www.webtechsource.com,have been working as a software engineer from last 4+ years. It's my hobby to learn new things and implement on live environment.

Leave a Reply

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

− 1 = 2

%d bloggers like this: