Skip Navigation

[Resolved] Filter view by a newly added checkbox

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

This support ticket is created 3 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.

Our next available supporter will start replying to tickets in about 0.68 hours from now. Thank you for your understanding.

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/Hong_Kong (GMT+08:00)

This topic contains 10 replies, has 2 voices.

Last updated by szymonF 3 years ago.

Assisted by: Luo Yang.

Author
Posts
#2212959

I have a new type of post on my site and I want it to only display in some views but not others. I created a custom field checkbox - the idea is that if it's not checked, the post displays everywhere, if it is, it only displays in a specific view.

The checkbox is set up to save '0' if it is not checked. I set up the view where checked posts are NOT meant to display, using a query filter:

Select items with field:
Hide Text is a number equal to 0

Unfortunately, the view doesn't display anything now. If I change the 0 to 1, it does display the texts that are checked (i.e. the ones that area meant to be hidden), so the filter works in principle. I suspect that the database simply hasn't been updated with a 0 for all the old records. How can I do that? Or if this isn't a problem, what is?

#2213505

Hello,

It is a known issue, see our erratum here:
https://toolset.com/errata/checkboxes-that-save-0-are-interpreted-as-checked-by-views/

The workaround is:
– change your checkboxes Field to save nothing to the database in case it’s not checked
– update all your posts so to reflect the new settings of the custom field

#2214553

Thanks for the quick reply! Is there some other, easier way to do it? Perhaps add a new taxonomy instead of a new custom field, and filter by that? I don't care what it is exactly, as long as it's something that gives me an on-off capability.

If that's not possible or won't make a difference, I would very much like the assistance mentioned in the thread you linked. I have over 1k posts, there is no way I am updating them all by hand, and applying the script that's linked in the posts sounds beyond my capability. I'd be most grateful for help with that.

#2215357

I don't think taxonomy will make it more easier, since you will need to migrate 1K posts custom checkbox fields to taxonomy term.
Please provide your website database dump file in below private message box, you can put the package files in your own google drive disk, share the link only, also point out the problem custom check box field URL, I need to test and debug it in my localhost, thanks
https://toolset.com/documentation/programmer-reference/debugging-sites-built-with-toolset/

#2217257

Thanks for the details, I am downloading the files, will update here if find anything

#2217263

I have checked it in your website database, you did not enable field "hide-me" in any post, so you don't need to change the database.

If you want to display the posts with field "hide-me" enabled, you can create a post view:
- Query posts
- Filter by:
Select items with field:
Hide me is a number equal to 1

If you want to display the posts without field "hide-me" enabled, you can try these:
1) create a post view:
- Query posts
2) Add below custom codes into your theme file "functions.php"

add_filter( 'wpv_filter_query', 'display_not_exist_posts', 99, 3 );
function display_not_exist_posts( $query_args, $view_settings, $views_id  ) {
    if ( $views_id == 123) { // if it is specific view and by default
        $query_args['meta_query'][] = array(
                'key'     => 'wpcf-hide-me', 
                   'compare' => 'NOT EXISTS',
        );
    }
    return $query_args;
}

Please replace 123 with your post view's ID

#2219973

Thanks, that works a dream!
Can I just check one more tiny thing - I have two views and a AP Archive that need to display posts without the "hide-me" enabled. The code for functions.php you sent - do I need to paste each separately for each view with the relevant view id, or is there some syntax to include the two views and archive in the same filter?

#2220839

For WordPress archive page, it needs to use pre_get_posts to add filters into the query, see WP document:
https://developer.wordpress.org/reference/hooks/pre_get_posts/

#2222471

I'm sorry, I don't really understand 🙁 Does this mean I need to use the code with the pre_get_query? Where does it go?
(I'm not that pro 😉 )

New threads created by Luo Yang and linked to this one are listed below:

https://toolset.com/forums/topic/filter-wordpress-archive-by-a-newly-added-checkbox/

#2223653

I assume the original questions of this thread is resolved, according to our support policy, we prefer one ticket one question.

For the new questions, please check the new thread here:
https://toolset.com/forums/topic/filter-wordpress-archive-by-a-newly-added-checkbox/

#2227151

Fantastic, thank you for your help and I'll look into the other forum now!