Skip Navigation

[Resolved] How to Add Custom Query for Views Sorting to Toolset?

This support ticket is created 6 years, 1 month 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.

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 Luo Yang 6 years, 1 month ago.

Assisted by: Luo Yang.

Author
Posts
#1123999

Hello,

We're trying to add custom sorting to the Toolset Views to help us output orders from orderpantry.com in a chronological order using Order Delivery Date Pro's time slots. We followed some similar topics in the support forums to see if we can add a custom query for sorting the orders, but haven't been able to get it to work.

We've tried to follow this thread, working off this code:

/**
 *  Custom sorting of appointments
 */
function custom_sort_appointments($query_args, $views_settings, $view_id) {
 
    if ('18' === $view_id) {
 
        $query_args['meta_query'] = array(
            'relation'  => 'AND',
          
            'date_q'    => array(
                'key'     => 'wpcf-date',
                'type'    => 'NUMERIC',
                'compare' => 'EXISTS',
            ),
          
            'practitioner_q'    => array(
                'key'     => 'wpcf-practitioner',
                'type'    => 'STRING',
                'compare' => 'EXISTS',
            )
        );
 
        $query_args['orderby'] = array(
            'date_q'            => 'DESC',
            'practitioner_q'    => 'ASC'
            );
 
    }
 
    return $query_args;
}
add_filter( 'wpv_filter_query', 'custom_sort_appointments', 101, 3);

And made this:

add_filter('wpv_filter_query', 'custom_order_due_sorting', 99, 3);
function custom_order_due_sorting($query_args, $view_settings, $view_id ) {
    if ( $view_id != 6041 ){
        return;
    }
    $meta_query = array();
  
    $meta_query[] = array('key' => 'Order Due Time');
    if ( !isset($query_args['meta_query']) ){
        $query_args['meta_query'] = array();
    }
    $query_args['meta_query'] = array_merge($meta_query, $query_args['meta_query']);
    add_filter('posts_orderby', 'kds_custom_order');
    return $query_args;
}
function kds_custom_order($orderby) {
    global $wpdb;
    return $wpdb->postmeta.'.meta_value ASC';
}

This doesn't actually work because it is using Toolset's own query. We'd like to create our own query and add it to Toolset. How can we do so? Any help would be appreciated.

#1124347

Hello,

I assume we are talking about the same problem of your previous post:
https://toolset.com/forums/topic/views-custom-sorting-not-working/

Views plugin is using wordpress WP_Query to query the posts, if you are going to order the result by multiple custom fields, please check wordpress document to setup your query parameters:
https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
section "'orderby' with multiple 'meta_key's"

And here is the document about the filter hook "wpv_filter_query":
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

If you still need assistance for it, since it is a custom PHP codes problem, please provide a test site with the same problem, also point out the problem page URL and view URL, I need a live website to test and debug.

#1125103

Thanks for the details, I can login your website, will update here if there is anything found

#1125106

I can see the problem in your website, but you are using 30+ plugins and custom codes with "Code Snippets" plugin, I need your permission to deactivate plugins and switch theme, please backup your website first.

And you can get the SQL query details by enable debug mode:
Dashboard-> Toolset-> Settings-> Front-end Content
in section "Debug mode", enable option "Enable Views debug mode"

#1125501

Hi Luo Yang,

We managed to get our custom sorting to work, which is great. However, the view doesn't keep our custom sorting after it updates because the view's page URL, staging1.orderpantry.com/index.php/div-search/, gets changed to staging1.orderpantry.com/wp-admin/admin-ajax.php. We can't have the URL change because our custom sorting check for the view's ID.

In the view's custom search settings, it is already set to "Do not adjust URLs after loading search results".

We're trying to find out why the change in the URL. Like you said, we have a lot of plugins and custom code to troubleshoot. Any help would be greatly appreciated!

We will have the website backed-up and ready before you hop on. Thank you!

#1125857

I assume you are using custom codes(JS) to sort the result, and it break the form from submitting:
hidden link

See those codes in "JS editor" of section "Search and Pagination", according to our support policy, we don't provide custom codes support:
https://toolset.com/toolset-support-policy/

As a workaround, you can edit above view, in section "Custom Search Settings", enable option "Full page refresh when visitors click on the search button", then after click the submit button, it will refresh the page, and run your custom JS codes again.

#1126352

Hi Luo Yang,

We'd like to keep the view on ajax refresh if possible.

I went ahead and removed the code inside Search and Pagination -> JS editor as well as those in our global JS that affects the view's page, and I still have the URL change to /wp-admin/admin-ajax.php, so this is definitely not from coming from our custom code. Can we please have this look at, at least to help identify the cause of the issue?

Thank you!

#1127306

I have tried these in your website:
1) Upgrade Types + Views plugin to the latest version, the problem persists
2) Deactivate all other plugins except Toolset plugins + Woocommerce plugin, switch to 2017 theme, the problem persists
3) Create a fresh view, query pages, test it in front-end, the problem persists:
hidden link

But I have tried the same setting in my localhost, there isn't similar problem, it works fine. see below test site:
hidden link
view URL:
hidden link
USER/PASS: xgren/111111

So it might be a server problem of your website, please check these:
1) Your web server meets the Toolset requirement:
https://toolset.com/toolset-requirements/
2) Try it in another webserver, and test again

#1128411
Screen Shot 2018-10-16 at 1.27.38 PM.png

Hi Luo Yang,

Today I tried to update the production site's Toolset Types and Views, but it keeps failing with an error message of:
Update package not available. ()

I've attached a screenshot of the error on the Toolset update page. I've unregistered and re-registered the website and checked licenses, all of which are valid.

Can we please have this sorted out, preferable before the end of today (October 16)? We need to have Toolset working properly so we can implement our custom KDS sorting on the website.

#1128805

I have installed duplicator plugin in your website, downloading the database dump file, will update as soon as possible.

#1128838

I have tried your website database dump file in my localhost, option "Do not adjust URLs after loading search results" works fine, so it should be a server problem, so it should be a server problem, please try to test your website in another web server, or let me know if you need a test server, I can setup a test site with your database dump file.