Skip Navigation

[Resolved] query filter for user role

This support ticket is created 5 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

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.

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

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 9 replies, has 2 voices.

Last updated by jozsefG 5 years, 1 month ago.

Assisted by: Waqar.

Author
Posts
#1218230

Hello

I have an archive and I want to set up to show only the posts of a given user role. But I don't see any option for that in the query filters. How can I achieve this?

#1218533

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Jozsef,

Thank you for contacting us and I'll be happy to assist.

Your understanding is correct and this kind of filtering is not available, but you're welcome to submit this as a feature request:
https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

Meanwhile, you can use "pre_get_posts" action ( ref: https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts ) and hook a custom function that first gets all the users from a particular role and then sets their IDs in the main query's "author__in" parameter ( ref: https://codex.wordpress.org/Class_Reference/WP_Query#Author_Parameters ).

This way only the posts from those specific authors will be included in that archive.

Here is a forum reply with a good example of a code snippet that you can use to get started:
https://wordpress.stackexchange.com/a/319304

Note: You'll need to use WordPress conditional tags ( ref: https://codex.wordpress.org/Conditional_Tags ), to make the condition in that code to be more precise so that it only targets the archive's that you want this filtering on.

For more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1218545

Hi Waqar

There was a solution for a problem that is somehow related to mine in this thread: https://toolset.com/forums/topic/conditional-display-based-on-user-role-of-post-author/

What do you think, could I use conditional somehow in the archive loop to show only the content that matches that one role?

#1218568

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Jozsef,

The approach suggested in that thread can also work, but it comes with a few downsides.

The archive view will be returning a fixed number of posts (for example "10"), but on the actual pages, visitors will be seeing fewer posts than that, since only posts with certain criteria will be shown and others will be ignored.

If there is pagination in use, the actual number of posts per page will vary inconsistently, as a result.

Also the output number of shortcodes "wpv-items-count" and "wpv-found-count" would also not match the actual number of posts shown in the archives.
( ref: https://toolset.com/documentation/user-guides/views-shortcodes/ )

If the above points are not a concern, then yes you can use the approach of using the "wpv-conditional" shortcode, for filtering out posts from other roles.
( ref: https://toolset.com/documentation/user-guides/conditional-html-output-in-views/ )

#1218804

Yes, you are right, and this could cause problems.

What do you think about this approach? https://toolset.com/forums/topic/filter-view-by-user-role-and-post-type/

#1218834

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Jozsef,

The approach suggested in this new thread ( https://toolset.com/forums/topic/filter-view-by-user-role-and-post-type/ ), is almost the same as what was shared in my earlier reply ( ref: https://toolset.com/forums/topic/query-filter-for-user-role/#post-1218533 ).

Since the view that you're using is not a post view but a WordPress archive view, "wpv_filter_query" can't be used and this is why I suggested using "pre_get_posts" instead.

Rest of the steps are pretty much the same.

#1218887

I tried to rewrite the code you recommended:

function remove_unvetted_authors( $query ) {
    if ( ! is_admin() && $query->is_archive() ) {
        $user_ids = get_users( [
            'role'   => 'producator_neautorizat',
            'fields' => 'ID'
        ] );

        $query->set( 'author__in', $user_ids );
    }
}
add_action( 'pre_get_posts', 'remove_unvetted_authors' );

Unfortunately it does exactly the opposite, it shows only those posts that are written bu authors of producator_neautorizat. I want to hide these posts from the archive. Please help me to understant what I am doing incorrectly...

#1218899

I changed it a little bit further, now it hides the post of the user from that role:

function remove_unvetted_authors( $query ) {
    if ( ! is_admin() && $query->is_archive() ) {
        $user_ids = get_users( [
            'role'   => 'producator_neautorizat',
            'fields' => 'ID'
        ] );

        $query->set( 'author__not_in', $user_ids );
    }
}
add_action( 'pre_get_posts', 'remove_unvetted_authors' );

I guess it is now correct, because as I understand it pre_get_post now shows authors that are not is that role.

But I have another problem now... Though I achieved not to show these posts in the archive now it disappears from the view that lists their own posts in the user's profile page (This user role is a temporary one, the admins first check the official documents they provide at registration and after that manually assign another user role: producator. That's why I need them to be able to create posts and show their own posts in the profile page but these will be public only when they get the producator user role.)

#1219394

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Jozsef,

Thanks for the update and glad you were able to adjust the code to work as needed.

If your goal is to make this filtering stop only for a specific page, you can use "is_page", conditional tag:
https://developer.wordpress.org/reference/functions/is_page/

For example, suppose that you don't want that filtering function to work on a page with ID '5'. In that case, you can update the condition in that function from:


if ( ! is_admin() && $query->is_archive() ) {

To


if ( ! is_admin() && $query->is_archive() && !is_page( 5 ) ) {

Feel free to replace 5 with the actual ID of the profile page on your website.

#1221420

Thank you, the problem is solved!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.