I have a database of maybe 3000 custom posts that I created with Types and Custom Fields. I am using Views to allow users to search through all the posts.
Let's say I create a custom field on my post type. The field is called "show to logged-out users".
Let's say for 100 of my posts, the field "show to logged-out users" = "yes".
Is there a way to only allow non-logged-in users to only search through these 100 posts? Essentially, we are limiting the scope of the search based on the value of a custom field.
To summarize:
3000 total posts.
Logged in users can search and view all 3000 posts.
Logged Out users can search and view only 100 of those 3000 posts.
Edit:
https://toolset.com/forums/topic/displaying-different-posts-and-custom-fields-based-on-user-role/
I asked a similar question here.
I can use conditional shortcodes to display posts based on the value of "show to logged-out users", but the pagination and search results numbers still reflect the actual number in the database, not the filtered amounts.
Hello,
I suggest you try these:
1) Create two post views:
a) post view ("for-logged-in-user"), without field filter "show to logged-out users" = "yes"
b) post view ("for-guest"), with field filter "show to logged-out users" = "yes"
2) Then in front-end, use [wpv-conditional] shortcode to check if current user is non-logged-in users or not, display different view shortcodes for him, see similar our document here:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/#63559
Checking User Information
Will option #1 maintain the correctresupt count and pagination?
I have only suggested only one option in above answer:
https://toolset.com/forums/topic/can-i-filter-search-results-based-on-custom-fields-and-user-roles/#post-1211671
I assume you are talking about:
a) post view ("for-logged-in-user") ...
Yes, it should be able to maintain the correct result count and pagination without any problem.
Can you explain how to set the "field filters" you mentioned above?
For example, you can try these:
1) Create a custom checkbox field "show to logged-out users", stores value "yes" when enabled, see screenshot checkbox.JPG
2) Create a post view "for-guest",
- query posts
- filter by:
Select items with field:
show to logged-out users is a string equal to yes
see screenshot field-filter.JPG
Then it should be able display posts with "show to logged-out users" checkbox enabled.
More help:
https://toolset.com/documentation/user-guides/filtering-views-by-custom-fields/
For your reference.
This was perfect -- I now have to views set up. They are exact duplicates of each other except that the Logged Out view only shows posts where the "Show To Logged Out Users" is checked. I then use access to render a view on the page based on a user's role. Simple and easy, thanks!