[Resolved] Sorting by number shows wrong results

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.

This topic contains 5 replies, has 2 voices.

Last updated by Ido Angel 5 months, 1 week ago.

Assigned support staff: Waqar.

Author
Posts
#1480825

Hi,

I have a custom field of "year of death".
Some of my entries are, thank god, still alive, and some are unfortunately not.
When I try to sort search resutls by "year of death" / ascending - the resutls include living people, aka people with "year of death" custom field empty.

Is there a way to simply not include entries with an empty custom field when I'm sorting?

thx!

#1482441

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Ido,

Thank you for contacting us and I'd be happy to assist.

On my test website, the posts without any date field values were automatically ignored, when front-end sorting controls were used for that data field.
( ref: https://toolset.com/documentation/user-guides/allowing-visitors-to-sort-the-front-end-results/ )

Can you please share temporary admin login details, along with the link to the page with this view, so that I can reproduce this on my test website?

Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.

regards,
Waqar

#1485029

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

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: hidden link

Case 2:
No custom field record exists for a post with specific key, at all.
Example screenshot: hidden link

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

#1485279

Thx very much Waqar!!
The code looks great.
Have you noticed on the site how the advanced search page is slow?
There are many filters there, bit still...
Any thoughts?
Thx!
Ido

#1490087

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Ido,

Thanks for writing back and glad that my message helped.

I've checked the search page and I didn't find it slow, considering the amount of content it needs to process and render.

To further improve the loading/rendering of the page in general, you can use the recommendations and suggestions from tools like:

hidden link
hidden link

If you come across any specific point which can be improved from the Toolset plugin's side, you're welcome to report it, through a new ticket.

regards,
Waqar

#1490091

My issue is resolved now. Thank you!