Skip Navigation

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

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 7.10 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
#2657349

Tell us what you are trying to do?

Continue this question: https://toolset.com/forums/topic/create-a-view-to-show-only-the-logged-in-user-own-posts/

Hi!

Mateus, can you help us with another feature?

Your code works well, but we need to allow users to access pages. Maybe I did not explained well before.

So, users must only view their own POSTS, but need to be allowed to view pages, because we will put forms and instruction on pages.

Could you help us, please?

#2657423

Mateus Getulio
Supporter

Languages: English (English )

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

Hello Gustavo,

Thank you for contacting us.

I made a small adjustment to that code to make it test if the CPT is from the type 'Page' before limiting the access:

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 or the post type is 'page'
        if (!current_user_can('activate_plugins') || $query->get('post_type') === 'page') {
            // If the current user is not an administrator or it's a 'page', don't modify the query
            return;
        }

        // If the current user is not an administrator and it's not a 'page', modify the query to show only their posts
        $query->set('author', $current_user_id);
    }
}
add_action('pre_get_posts', 'display_user_posts_only');

Can you please test it and tell us if it has the expected effect?

Thank you, please let us know.

#2657475

Hi! Mateus.

No, it does not work anymore.

An author now can see the pages, but can see all posts also.

So, it sees that this code has no function, since WP is now working as always, everyone can see everything.

#2657509

Mateus Getulio
Supporter

Languages: English (English )

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

Sorry for the confusion.

I worked in following code, tested it locally and it is working as expected:

function restrict_post_access_and_list($query) {
    // Check if the post type is "page." Allow access to all users for pages.
    if ($query->is_page) {
        return;
    }

    // If it is not CPT "page" and user is not logged in, send them to the login page
    if (!is_user_logged_in()) {
        wp_redirect(wp_login_url(get_permalink())); // Redirect non-logged-in users to the login page.
        exit;
    }

    if (is_admin() || !$query->is_main_query()) {
        return;
    }

    // Check if the user is an administrator.
    if (current_user_can('administrator')) {
        return;
    }

    // Check if the query is for a single post.
    if ($query->is_single) {
        $post_id = $query->get('p'); // Get the post ID.
        $post = get_post($post_id); // Get the post object using the post ID.

        if ($post && is_user_logged_in() && $post->post_author == get_current_user_id()) {
            return;
        } else {
            wp_redirect(home_url()); // Redirect to the homepage for unauthorized users.
            exit;
        }
    }

    // Check if the query is for an archive or loop.
    if ($query->is_home() || $query->is_archive()) {
        $current_user_id = get_current_user_id();
        $query->set('author', $current_user_id);
    }
}

add_action('pre_get_posts', 'restrict_post_access_and_list');

We wanted to let you know that while we have provided some initial assistance with your website, our support is limited to certain scope and guidelines.

We have helped with a some custom code snippet for your WordPress website to address your specific requirements.

It's important to understand that custom coding, debugging, and any further modifications to the code are considered out of the scope of our support. While we are here to provide initial guidance and assistance, any additional development or customization needs may require the expertise of a professional contractor.

To assist you further with your project, we recommend considering the services of a skilled contractor who specializes in WordPress development. You can find experienced WordPress contractors available for hire through the following link: (https://toolset.com/contractors/).

These contractors have the expertise and experience to provide tailored solutions to meet your specific requirements and ensure the long-term success of your website.

We hope that the code snippet we provided serves as a helpful reference for your project and that you can find the right contractor to assist you further. If you have any questions or need additional guidance with using Toolset, please feel free to reach out, and we'll do our best to assist you.

Best regards,

Mateus

#2657757

Thank you brother.

It works.

If I could suggest you, to create a View tool which shows users only their posts, would be great.

There is only one plugin I know which do something like this.