Skip Navigation

[Resolved] Filter Out Hidden Posts

This support ticket is created 4 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 12 replies, has 2 voices.

Last updated by scarletW 4 years ago.

Assisted by: Christian Cox.

Author
Posts
#1540827
hidden.jpg

I have some products that I will eventually have in which I intend to hide and password protect on my website. I saw the custom field for 'hidden' in the list of filters in WP Views, but I have no idea what to put in the variables to hide those hidden posts. I've attached a screenshot. What do I select and put to HIDE these hidden posts from showing up in WP Views (like the one on my main page: fantaproductions.com).

Thanks!
-Scarlet

#1541037

Hi, it depends on what you store in the "hidden" field to indicate a hidden post. I assume this is a field you created in Types, correct? Is it a checkbox, or a radio button, or a select field? Something else? Can you show me a screenshot of the field settings in Toolset > Post Fields? Open up all the settings so I can see the configurations for the field and include that screenshot in your next reply.

#1541043
custom fields.jpg

I actually just selected it from the automatic fields listed. I didn't add a custom field. Do I need to add a custom field so I can hide hidden products in WP Views? I've attached the screenshot I think you were asking for.

#1541067
Screen Shot 2020-03-05 at 11.47.38 AM.png
Screen Shot 2020-03-05 at 11.47.32 AM.png

If you didn't create the "hidden" field, then I wouldn't try to rely on it for determining which posts to hide. You should create your own field, like a radio button with two options. One option should be "show" (set as default) and the other option should be "hide". I'm attaching a screenshot here. Now when you create a post it will be set to "show" by default, or you can choose to hide it.

Then in your View's query filters, you will choose your new field as the filter source, as a "number" equal to the Constant "1". That is because we used the number 1 in the field options as the custom field content. Only those posts with the field set to "show" will be displayed in the results. You may have to resave existing posts so they have the correct saved value.

#1541087

Thank you for the reply. I will try this. I don't have any products that are hidden yet, so I won't have to worry about going back and resaving any. Let me try this and see if it works.

#1541125

So, I guess I'm not doing this right. 🙁 I still get a message saying "This content is password protected" where that product should be in the view.

#1541127
views result.jpg

So, when I add this type of product, I am also password protecting it and hiding it from the general catalog. It does not show up in my normal products display because of this. However, when I do the steps you showed above and apply the filter, it still shows a placeholder for the product (see image).

#1541329

Okay if you're password-protecting a post in WordPress, the standard behavior in Views is to show this password field instead of the post body. The post title and post link should be visible, though, as the standard behavior in a post archive. There isn't a query filter available to filter these out. If you want to completely remove this post body placeholder from a View, then you'll need to implement a custom field that stores a value for show or hide, as I already described, and filter this post out of the results. So both the password protect custom field and the password protect option in WordPress should be implemented.

#1541377

Okay thanks. Doing something like that is a little over my head, so I will figure out a different solution when the time comes.

#1543805

I'd really like to help you figure it out! I'll be glad to set it up for you in a test site so you can see how everything works together, then help you if you need help on your own site. We have a test site sandbox set up at https://discover-wp.com, where you can sign up for a free account. If you sign up for a free account using the same email address you used for your account here, I can send you an invitation to collaborate on a test project.

#1551629

Well, if it still shows the password box for the password protected products in the View where I don't want them to show up, I'm not really sure I can work around that. I might just do a separate site for those types of products.

#1552601
Screen Shot 2020-03-16 at 1.35.13 PM.png

Oh it turns out there is a simpler solution that I overlooked. I did a bit of digging and found out that there is a way to filter these password-protected posts from the View results by including a bit of custom code. Here is a code snippet for you to use:

function tssupp_no_password_protected_posts($query_args, $view_settings, $view_id)
{
    $view_ids = array( 123, 456, 789 ); // Comma-separated list of View IDs you want to apply this filter to

    if ( in_array($view_id, $view_ids) ) {

        $query_args['has_password'] = false;
    }
    return $query_args;
}
add_filter('wpv_filter_query', 'tssupp_no_password_protected_posts', 99, 3);

You should update the code to replace 123, 456, 789 with a single View ID, or a comma-separated list of View IDs. Any View ID you add here will be adjusted so that no password-protected posts are displayed in the results. That's the only thing you should modify in this code. You can place the code in a child theme's functions.php file, or you can add it to the end of a new code snippet in Toolset > Settings > Custom Code. I have included a screenshot here showing a custom code snippet fully configured with this code, so you can see the snippet configurations. After you save the updated snippet, be sure to activate it. Then test out the View on the front-end of the site.

#1566607

Thank you for the solution, but I decided to go with a separate website for those products.

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