I am trying to adjust a Query Filter to test if a Custom Field is not set. However, I cannot seem to get this to work. Is there anything you recommend?
There's not a way to accomplish this simply in wp-admin, but I can show you how to set up some custom code to handle this for you. First, remove any Query Filter you have applied to test the custom field value in the View editor. Then add this custom code to your child theme's functions.php file, or create a new snippet in Toolset > Settings > Custom Code:
add_filter( 'wpv_filter_query', 'get_books_without_zip', 10, 3);
function get_books_without_zip( $query_args ,$view_settings, $view_id ) {
$views = array( 1234 );
if (in_array($view_id, $views)) {
$args = array(
array(
'key' => 'wpcf-your-field-slug',
'compare' => 'NOT EXISTS',
'value' => '',
)
);
$query_args['meta_query'] = isset($query_args['meta_query']) ? $query_args['meta_query'] : [];
$query_args['meta_query'][] = $args;
}
return $query_args;
}
Replace 1234 with the numeric ID of this View, and replace your-field-slug with the slug of your custom field as shown in wp-admin. Keep the wpcf- prefix. So if your field slug in wp-admin is "shirt-size" then you would use "wpcf-shirt-size" as the key.
Thanks Christian. I ended up just setting up a new sub-category and assigning the product accordingly. This way I was not overriding functionality on the site. This is now working properly in my view.
However, one thing I did notice that the new sub-category is not showing up in the drop-down within my custom search. I have previously added other sub-categories and they show up fine, but for some reason this new one is not.
You can see the search page here... hidden link
There should be a sub-category for "Pigments". But it is not displaying. I know that it is indeed working as you can see the view here... hidden link
Any ideas on what might be causing this?
Appreciate the help!
Chris
Let's address that issue in a separate ticket, since it's a bit different from the original problem here. Thanks for helping us keep things organized 🙂