[Resolved] Show posts to author and admin

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 15 replies, has 2 voices.

Last updated by Shane 1 month ago.

Assigned support staff: Shane.

Author
Posts
#1508837

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?

#1509077

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Katja,

Thank you for getting in touch.

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.

Have a look at the link below for a guide on how to use our access shortcode.
https://toolset.com/documentation/user-guides/access-control/access-control-texts-inside-page-content/

Please let me know if this helps.

Thanks,
Shane

#1509391

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Katja,

The only other way is by utilizing the view hook to check the user role and query based on that.

So what you will need to do is use the hook below.
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

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.

Thanks,
Shane

#1515625

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.

#1516429

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Katja,

Try using this code below.


//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

#1520085

Thank you Shane. Unfortunately this doesn't change anything, still the admin can't see all posts in frontend.

#1520869

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Katja,

Would you mind allowing me to have admin access to the website so that I can further debug this one for you ?

Also let me know where the view is you are testing.

Thanks,
Shane

#1521281

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Katja,

I made some changes to the code.

Could you check your page now and let me know if he view works correctly?

Thanks,
Shane

#1521373

Hia Shane. Now every user sees everyone`s posts.

#1521565

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Katja,

Is the current user that i'm using an admin role ?

Also could you provide as well an example login for a user that is not an admin so that I can verify the code on the non-admin users as well.

Thanks,
Shane

#1522129

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".

#1525349

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Katja,

It seems there is a fatal error being thrown on the site at the moment.

Would it be possible to get FTP access to check and debug this as it is related to the custom code .

Thanks,
Shane

#1526155

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;
}

#1527007

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Katja,

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.

Thanks,
Shane

#1527027

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.