I want to show the author's posts (and children/related) to the logged in user and also to admin. So the user would see only his posts, but admin everyone's. Now I thought this would be default but apparantley it is not or I'm doing something wrong since the admin can't see the all the posts. He sees also only his own.
The query filter is "Select posts with the author the same as the current logged in user."
How should I modify it to show the posts also to admin?
The issue is actually because of this filter here "Select posts with the author the same as the current logged in user."
Now on the frontend its not possible to achieve this.
There is a way to work around this using the conditional shortcode but it can have an effect of the page's load speed if there is alot of posts.
That is because the conditional code will need to check every post that is being listed for the matching conditional criteria in order for it to display.
Another way is just to create 2 views and one only displays when the user is admin and the other displays for non-admins.
You can actually use our access shortcode to switch between them.
To hook into the view query to filter it based on the current user or the admin.
So if the admin is logged in then you will just return the view normally, however if its a non admin user you will modify the query based on that user so only their posts will be returned.
Please let me know if this points you in the right direction.
Thank you Shane. Yes, I'm sure this points me to right direction, but since I'm no php coder I don't quite know how to create the function to check the logged in user and modify te query. But perhaps this goes beyond your support policy, I understand that if it's the case.
//Return only posts from the current author when listing posts of type company:
add_filter( 'wpv_filter_query', 'prefix_show_only_current_author', 99, 3 );
function prefix_show_only_current_author( $query_args,$view_id,$view_settings ) {
global $current_user;
if ( !is_admin() && $view_id == 1234 ) {
$query_args['author'] = empty( $current_user->ID ) ? -1 : $current_user->ID;
}
return $query_args;
}
Replace the 1234 with the ID of your view and let me know if this helps.
Thanks,
Shane
Yes Shane, of course your user is admin user. Now you can log in also with test user "123" and password "test". This user should see only his own post "Testiasiakas".
Hi Shane, I deleted your custom filter php file, everything works again. Here below is the code you had. Perhaps I will have to think some workaround for this. For example nest the views so that one view (A) shows all and this if for admin.
And then another view (B) and put A inside it and in B filter to show only logged in author's posts. This would be for normal users. And this way we only have one view to modify and modifications would show in both views. But perhaps the B's query wouldn't apply to A?
<?php
/**
* New custom code snippet (replace this with snippet description).
*/
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
// Put the code of your snippet below this comment.
//Return only posts from the current author when listing posts of type company:
add_filter( 'wpv_filter_query', 'prefix_show_only_current_author', 99, 3 );
function prefix_show_only_current_author( $query_args,$view_id,$view_settings ) {
global $current_user;
if ( !is_admin() && $view_id == 68 ) {
$query_args['author'] = empty( $current_user->ID ) ? -1 : $current_user->ID;
}
return $query_args;
}
This would be the exact same as I mentioned previously about creating 2 views and setting one to display everything for admins and only the relevant posts for non-admins.
Hi Shane,
I wouldn't think so, because you didn't mention at all the nesting of views. You just talked about 2 different views which would mean double work when modifying views. I will try one view inside another = nested view = only 1 view to modify.
But yeah, I'm still not sure if the query filter works (as I mentioned in my previous post) with nested views. But I will try.