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?
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
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.
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/
Thanks for the details, I am downloading the files, will update here if find anything
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
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?
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/
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 😉 )
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/
Fantastic, thank you for your help and I'll look into the other forum now!