Hi Ido,
Thank you for sharing the admin access.
First, it is important to understand the difference between the empty and non-existent custom field values or records.
Case 1:
A custom field record exists for a post with specific key, but the value for the record is empty/null.
Example screenshot: enlace oculto
Case 2:
No custom field record exists for a post with specific key, at all.
Example screenshot: enlace oculto
In the WordPress query, if a post doesn't include any custom field record for a key (case 2), it is ignored when that custom field is used for sorting order or filtering.
Running some tests on your website's data through a test view "Temp view from TS support" and page "View: Temp view from TS support ", it seems all 1355 "ערכים" posts on your website have a custom field entry/record with key "wpcf-year-of-death", even though some of them have empty values (case 1).
( likely because you imported these posts and they were not added manually through the post edit screen )
As a result, if you'd like to remove those posts which have an empty custom field value for the field "wpcf-year-of-death" when sorting by this field, you'll need to adjust the query through the "wpv_filter_query" filter:
( ref: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query )
function filter_sorting_fields_default($view_args, $view_settings, $view_id) {
// if specific view
if (in_array($view_id, array(1234))) {
// slug of the target field
$target_field_slug = "wpcf-year-of-death";
// check if a user filter exists for the target field
if(!empty($view_args['meta_query'])) {
foreach ($view_args['meta_query'] as $meta_query_arr ) {
if( (!empty($meta_query_arr['key'])) && (isset($meta_query_arr['key'])) ) {
$available_keys[] = $meta_query_arr['key'];
}
}
}
else {
$available_keys[] = '';
}
// if no user filter for the target field exists and the sort order is set to use the target field add a fixed filter
if ( (!in_array($target_field_slug, $available_keys)) && ($_GET['wpv_sort_orderby'] == 'field-'.$target_field_slug) ) {
$view_args['meta_query'][] = array(
'key' => $target_field_slug,
'value' => 1,
'type' => 'CHAR',
'compare' => '>='
);
}
}
return $view_args;
}
add_filter('wpv_filter_query', 'filter_sorting_fields_default', 99, 3);
In this example snippet, the filter executes for the view with ID "1234", and if sorting order is set to use the target field and no custom field filter exists from the user for that field, it add a custom filter to bring in only those posts where the value is greater than or equal to 1.
I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/
regards,
Waqar