Skip Navigation

[Resolved] Create a view to show only the logged-in user own posts

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.

Our next available supporter will start replying to tickets in about 8.80 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Sao_Paulo (GMT-03:00)

This topic contains 3 replies, has 2 voices.

Last updated by Mateus Getulio 1 year ago.

Assisted by: Mateus Getulio.

Author
Posts
#2654631

Tell us what you are trying to do?
Is it possible to create a blog view which shows only own user posts? Each user must create posts, but only be able to see their own, never others.

Is there any documentation that you are following?
Yes, documentation here.

Is there a similar example that we can see?
I don't know.

What is the link to your site?
hidden link

#2654697

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello,

Thanks for reaching out to our support.

You may want to try the Toolset Access plugin, which allows for controlling the permissions of different user roles in relation to post viewing, editing, and publishing. You can find more information about it here: https://toolset.com/course-lesson/setting-access-control/

With this method, posts created by others won't be visible in the front end although they will still appear in the admin area list, only they won't be editable.

If you're interested in concealing posts from other authors in the admin area list as well, you can try the following code snippet from this tutorial: hidden link

function posts_for_current_author($query) {
    global $pagenow;

    if( 'edit.php' != $pagenow || !$query->is_admin )
        return $query;

    if( !current_user_can( 'edit_others_posts' ) ) {
        global $user_ID;
        $query->set('author', $user_ID );
    }
    return $query;
}
add_filter('pre_get_posts', 'posts_for_current_author');

You can add the above code snippet using either Toolset's custom code feature (ref to: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/) or by adding it to your active theme's "functions.php" file.

I hope this information is useful. For more personalized guidance on custom code or related queries, you might also consider consulting the services of a professional from our recommended contractors list: https://toolset.com/contractors/

Best regards,
Mateus

#2655231

Hi Mateus!

Ok, maybe I understand what you said.

But this method only restrict access by user role, not by user login, right?

In our site case we have dozens of users, and each one must be allowed to view only their own posts, never others.

Your method only restrict access by group, am I right?

Therefore, we need that all interaction must be done in frontend, not in wp-admin.

Could you please confirm to me if I understood right or not?

#2655329

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hey there,

That's right, Toolset Access restricts access by user roles rather than individual user logins.

In your case, where each user must be able to view only their own posts on the frontend and not others, you would likely need a more custom solution. This typically involves creating a custom query or filtering mechanism in your theme or using a custom plugin to achieve this functionality.

You can try using the following code to limiting the posts based on specific users:

function display_user_posts_only($query) {
    if (is_user_logged_in() && $query->is_main_query()) {
        // Get the current user's ID
        $current_user_id = get_current_user_id();

        // Check if the current user is an administrator
        if (!current_user_can('activate_plugins')) {
            // If the current user is not an administrator, modify the query to show only their posts
            $query->set('author', $current_user_id);
        }
    }
}
add_action('pre_get_posts', 'display_user_posts_only');

This code will:

- Check if a user is logged in and if it's the main query on the frontend.
- Get the current user's ID using get_current_user_id().
- Check if the current user is an administrator using current_user_can('activate_plugins'). Administrators typically have the capability to activate plugins, so this capability is commonly used to identify administrator users.
- If the current user is not an administrator, the query is modified to show only their posts in the frontend

Please give it a try and tell us how that goes for you.

Best,

#2655985

Thank you brother.

Apparently it solves the issue.

Have a good week!