Hello,
Tell us what you are trying to do?
I would like to create a VIEW which displays only posts (CPT "fotograaf") created by users with the role "Author". All other posts created by users with the role "Administrator" or "Editor", or any other role, should not be displayed in the LOOP. Is there a way to filter out the correct posts?
Is there any documentation that you are following?
I have found similar question on the support forum, but I am unsure if this is exactly the same.
https://toolset.com/forums/topic/create-a-view-that-shows-posts-authored-only-by-users-with-a-specific-role/
Is there a similar example that we can see?
no
What is the link to your site?
hidden link
thank you
Minesh
Supporter
Languages:
English (English )
Timezone:
Asia/Kolkata (GMT+05:30)
Hello. Thank you for contacting the Toolset support.
There is no native filter to filter the posts based on the role. The ticket you shared is the workaround I shared:
- https://toolset.com/forums/topic/create-a-view-that-shows-posts-authored-only-by-users-with-a-specific-role/#post-2116151
The workaround is you will have to add the following code to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/
function func_filter_view_basedon_role( $query_args ,$view_settings, $view_id ) {
global $post;
if ( $view_id == 99999) {
$args1 = array( 'fields'=>'ID',
'role' => 'author',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$found_user_ids = get_users($args1);
$query_args['author__in '] = (!empty($found_user_ids))? $found_user_ids:array(0);
}
return $query_args;
}
add_filter( 'wpv_filter_query', 'func_filter_view_basedon_role', 10, 3);
Where:
- Replace 99999 with your original view Id.
- As you are see we find first all user IDs belongs to role 'author' and we hook into the "author__in" argument the found user IDs.
More info:
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
Hello Minesh,
When I run script below:
function func_filter_view_basedon_role( $query_args ,$view_settings, $view_id ) {
global $post;
// Fotografen-view-frontpage = view_id 1241
if ( $view_id == 1241) {
echo "hello, found view_id 1241";
$args1 = array( 'fields'=>'ID',
'role' => 'author',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$found_user_ids = get_users($args1);
$query_args['author__in '] = (!empty($found_user_ids))? $found_user_ids:array(0);
?><pre><?php var_dump($query_args); ?></pre><?php
}
return $query_args;
}
add_filter( 'wpv_filter_query', 'func_filter_view_basedon_role', 10, 3);
I can see output below:
Array(11) {
["post_type"]=>
array(1) {
[0]=>
string(9) "fotograaf"
}
["paged"]=>
string(1) "1"
["suppress_filters"]=>
bool(false)
["ignore_sticky_posts"]=>
bool(true)
["ep_integrate"]=>
bool(false)
["posts_per_page"]=>
int(10)
["wpv_original_limit"]=>
int(-1)
["wpv_original_offset"]=>
int(0)
["wpv_original_posts_per_page"]=>
int(10)
["post_status"]=>
array(1) {
[0]=>
string(7) "publish"
}
["author__in "]=>
array(1) {
[0]=>
string(1) "2"
}
}
But in the VIEW loop I still see post from role "Administrator". So somehow the the loop is not filtered on post created by Authors only.
Is the only action I have to do to put the code to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/ or need I to do something else as well?
Minesh
Supporter
Languages:
English (English )
Timezone:
Asia/Kolkata (GMT+05:30)
Can you please share problem URL where you added the view as well as admin access details.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
Minesh
Supporter
Languages:
English (English )
Timezone:
Asia/Kolkata (GMT+05:30)
I checked now I can see only 2 posts displyed under section "onze leden":
=> hidden link
I can see both post's author is role "author".
For my information, how can I see the role of the specific post?
Minesh
Supporter
Languages:
English (English )
Timezone:
Asia/Kolkata (GMT+05:30)
There is no role assigned to post. You can see author assigned to post and role assigned to current user.
To display author assigned to post, you can use shortcode: [wpv-post-author format="meta" meta="nickname"]
=> https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#vf-154504
To display role of current loggedin user, you can use shortcode: [wpv-current-user info="role"]
=> https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#vf-153354
Hello Minesh,
Thank you for the shortcodes.
I have created a post as Administrator. Although I can see the post on the front-end, which is not what I would expect. My intention is to display only posts created by role "Authors".
Attached you can see in the image which post I just created.
Minesh
Supporter
Languages:
English (English )
Timezone:
Asia/Kolkata (GMT+05:30)
I've adjusted the code as given under to the "Custom Code" section:
function func_filter_view_basedon_role( $query_args ,$view_settings, $view_id ) {
global $post;
// Fotografen-view-frontpage = view_id 1241
if ( $view_id == 1241) {
$args1 = array( 'fields'=>'ID',
'role' => 'author',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$found_user_ids = get_users($args1);
$query_args['author__in'] = (!empty($found_user_ids))? $found_user_ids:array(0);
}
return $query_args;
}
add_filter( 'wpv_filter_query', 'func_filter_view_basedon_role', 99, 3);
I can see now only two posts are displayed within the section "onze leden": hidden link
Hello Minesh,
Yes it works! Thank you very much for your help!