I am trying to: have a View omit private (password protected) custom posts.
I expected to see: an option in the Add Filter > Post Status function to exclude by type
Instead, I got: Checkboxes to INCLUDE (not EXCLUDE)
"Check statuses
publish, pending, draft, auto-draft, future, private, inherit, trash, any"
Is there any way to make this filter an EXCLUDE rather than INCLUDE?
Well, that's bottom line the same, isn't it?
You can filter Views Queries by Post Status.
This allows you to load only contents of the targeted post status and also include the custom post statuses.
So if you want a View to return let's say all Post status but not the Private ones, just check all boxes without the one you will not have in the Loop.
The thing is, Password Protected and Private are not the same.
If you edit a post you can either Password Protect it privately publish it.
Password is not a post status as per WordPress native Post status definition.
https://codex.wordpress.org/Post_Status
These are:
Publish
Future
Draft
Pending
Private
Trash
Auto-Draft
What you can do in case of Password Protected (post status Publish), is create a Custom ShortCode that checks if the Post is password protected.
You can do this with the WordPress native function post_password_required( $post )
In the ShortCode you will just check the current posts password requirements and as example return "true" if the post is protected.
Then, with a Conditional HTML using wpv-conditional in Views, you use this Custom ShortCode to evaluate the Posts and dynamically display them or not.
The DOC to create this code is here:
https://codex.wordpress.org/Function_Reference/post_password_required
https://codex.wordpress.org/Shortcode_API
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/
https://toolset.com/documentation/user-guides/filtering-views-query-by-post-status/
Forgive me because my coding strength is CSS, not PHP. So I may be stumbling on the basics here.
I'm trying this code:
<wpv-loop>
[wpv-conditional if="( 'has_password() eq 'false' )"]
LOOP OUTPUT FOR CPTS
[/wpv-conditional]
</wpv-loop>
Using this boolean here: http://codex.wordpress.org/Class_Reference/WP_Query#Password_Parameters
It's not working and I figure my mediocre PHP skills are in the way. Can you point me further in the right direction?
No, what I meant is to create a ShortCode following the WordPress API Codex:
https://codex.wordpress.org/Shortcode_API
In the ShortCode you evaluate this function:
https://codex.wordpress.org/Function_Reference/post_password_required
A example ShortCode as above described looks like this:
//Returns (boolean) false if a password is not required or the correct password cookie is present, true otherwise.
function check_pass_protection_func( $atts ){
//create our return $variable and use the get_the_ID () to get the Posts ID
$is_protected = post_password_required( get_the_ID () );
//return the $variable
return post_password_required( $post );
}
add_shortcode( 'is-protected', 'check_pass_protection_func' );
Register that ShortCode in Views > Settings > Compatibility > 3rd Party ShortCodes and then apply it in a wpv-conditional as the example below:
https://toolset.com/documentation/user-guides/shortcodes-within-shortcodes/ > Third-party shortcode arguments
//Returns posts that are NOT password protected
[wpv-conditional if="( '[is-protected]' ne '1' )"]
Display things
[/wpv-conditional]
//Returns Posts that ARE password Protected
[wpv-conditional if="( '[is-protected]' eq '1' )"]
Display things
[/wpv-conditional]
Please let me know if the above solution works for you, I look forward to your reply!
Thank you for your patience.
This works beautifully. And I found a functions.php plugin to use so I don't have to worry about theme updates. Thanks for the lesson in custom shortcoding, I learned a lot.
HI beda,
when I apply this solution to my search view under the loop output I get an empty space instead of the protected posts. is there a way to totally remove the protected posts from the search results view without having blank spaces ?
thanks,
David