Skip Navigation

[Resolved] Omit Private Posts From View Via Filter

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to omit posts from a View that are Password Protected.

Solution:
1. Create a ShortCode following the WordPress API Codex:
https://codex.wordpress.org/Shortcode_API

2. In the ShortCode you evaluate this function:
https://codex.wordpress.org/Function_Reference/post_password_required

Example ShortCode: the first block of code in this answer: https://toolset.com/forums/topic/omit-private-posts-from-view-via-filter/#post-367945

3. 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
Example: the second block of code in this answer:https://toolset.com/forums/topic/omit-private-posts-from-view-via-filter/#post-367945

This support ticket is created 8 years 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Tagged: 

This topic contains 5 replies, has 3 voices.

Last updated by davidZ-4 8 years ago.

Assisted by: Beda.

Author
Posts
#367384

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?

#367477

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/

#367822

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?

#367945

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.

#368253

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.

#471412

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

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