Skip Navigation

[Resolved] View custom search issue with sorting by 2 custom fields

This support ticket is created 3 years, 5 months 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 8 replies, has 5 voices.

Last updated by Nigel 3 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#1841865

Hi, I needed to sort my view by 2 custom fields, so I setup this custom code:

add_filter('wpv_filter_query', 'thickd_widthd_asc', 199, 3);
function thickd_widthd_asc($query_args, $view_settings, $view_id ) {
  $view_ids = array( 64433 );
  if( in_array( $view_id, $view_ids)) {
    $query_args['meta_query'] = array(
        'relation' => 'AND',
        'thickD_clause' => array(
            'key' => 'wpcf-mouldings-thickness-d',
            'compare' => 'EXISTS',
            //'type' => 'NUMERIC',
        ),
        'widthD_clause' => array(
            'key' => 'wpcf-mouldings-width-d',
            'compare' => 'EXISTS',
            //'type' => 'NUMERIC',
        ),
    );
    $query_args['orderby'] = array(
        'thickD_clause' => 'ASC',
        'widthD_clause' => 'ASC',
    );
  }
  return $query_args;
}

It works fine, but now the problem is that the custom search is not working. The view has a filter by category, it doesn't filter the result, I still get all the items. If I deactivate the custom code it works fine. Can you help me?

thanks

#1842239

Hi, can you enable Views debug mode and also use error logging to log the $query_args variable before it is returned in this callback function? I would like to compare the contents of the $query_args parameter and the final Query Args from the Views debug panel. You can turn on Views debugging in Toolset > Settings > Frontend Content > Debug Mode. You can add error log statements to the PHP code like so:

...
error_log('query args for View ID 64433: ');
error_log(print_r($query_args, true));
error_log('query args complete');
return $query_args;

If you're not familiar with error log files, I can show you how to activate one temporarily. Go in your wp-config.php file and look for

define('WP_DEBUG', false);

Change it to:

define('WP_DEBUG', true);

Then add these lines, just after the WP_DEBUG line:

define('WP_DEBUG_LOG', dirname(__FILE__) . '/error_log.txt');
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );
define('WP_DISABLE_FATAL_ERROR_HANDLER',true);

After you have added the PHP error_log code, activated an error log, and turned on Views debug mode, load the page containing this View on the front-end of your site. You should see a popup with the Views debug information. Please open up the Query Args section corresponding to this View ID, and copy the information you see there. Paste it in your next reply. Then check the error log file. If you used the code above to generate an error log file, you will find it at error_log.txt in your site's root directory. Use FTP to look for this file in the same directory as wp-config.php. You may need to click "Refresh" in your FTP browser to see a new file appear. Please download this file to your computer, open it in any text editing software, and send me its contents. Once that is done, you can revert the changes you made to wp-config.php and delete the log file using FTP.

#1842353

Okay thank you, the logs show me that there are no tax query parameters in the query at the time of wpv_filter_query. I see meta_query, but no tax_query here, which would be present if the taxonomy filter criteria were being passed into the wpv_filter_query function:

Array
(
    [post_type] => Array
        (
            [0] => custom-wood-moulding
        )
 
    [paged] => 1
    [suppress_filters] => 
    [ignore_sticky_posts] => 1
    [ep_integrate] => 
    [posts_per_page] => 39
    [post__not_in] => Array
        (
            [0] => 59461
        )
 
    [wpv_original_limit] => -1
    [wpv_original_offset] => 0
    [wpv_original_posts_per_page] => 39
    [meta_query] => Array
        (
            [relation] => AND
            [thickD_clause] => Array
                (
                    [key] => wpcf-mouldings-thickness-d
                    [compare] => EXISTS
                )
 
            [widthD_clause] => Array
                (
                    [key] => wpcf-mouldings-width-d
                    [compare] => EXISTS
                )
 
        )
 
    [post_status] => Array
        (
            [0] => publish
            [1] => private
        )
 
    [meta_key] => wpcf-mouldings-thickness-d
    [orderby] => Array
        (
            [thickD_clause] => ASC
            [widthD_clause] => ASC
        )
 
    [order] => ASC
)

So I'd like to know more about how the View with the taxonomy filter is configured. Is it created in Blocks or in legacy Views? If it is created in legacy Views, please edit this View and find the Query Filter section. If it is not displayed, use the Screen Options tab in the top right corner to expose the Query Filter panel. Look for the Query Filter configurations that correspond to your taxonomy filters. Open those configurations and take screenshots so I can see the settings. Include those screenshots in your next reply.

It is also possible that your theme or another plugin is affecting the query before the wpv_filter_query hook is triggered. To rule this out, please temporarily disable all non-Toolset plugins, deactivate any active custom code snippets in Toolset > Settings > Custom Code, and activate a default theme like Twenty Twenty. If you need to activate maintenance mode using a plugin that's fine. Test again and observe the results. If the problem is resolved, reactivate your theme, other plugins, and custom code snippets one by one, testing each time until the problem returns. Let me know what you find out.

#1842375
view.PNG
block.PNG

Hi, I've tested deactivating all the plugins and using the default theme, I still get the problem if the wpv_filter_query filter is activated. I tested with a Block view and with a legacy view, I get the same problem with both.

I attach screenshots of both view filters.

#1842487

I see now. In the Query Filter section in the legacy View editor, no taxonomy Query Filters exist. I see only the custom field filter. This is why no taxonomy filter parameters are passed into the wpv_filter_query function. Even though you may have filter controls in the Search and Pagination panel, there must be corresponding Query Filters. This may have happened if you copy + pasted the filter controls from another View, or for some unexpected reason I cannot explain. I suggest you continue by editing the View in the legacy View editor so you have more control over the filters. Then you can add the Query Filters as needed using one of the following methods:
- Delete the taxonomy filter controls from the Search and Pagination panel and recreate them. The required Query Filter configurations will be automatically created in sync with the new filter controls.
- Or, manually add the taxonomy Query Filters by clicking "Add a filter" in the Query Filter section. You must manually set the URL parameters for each taxonomy Query Filter to match the URL parameters defined in the shortcodes of existing taxonomy filter controls in the Search and Pagination panel.

#1842567

Sorry Christian, there was a mistake. In my first message I wrote that we want to filter by category, but it is not true. We are filtering by the custom text field "Moulding Type Name". It is the filter that is not working properly when the custom code is activated. It works fine if I remove the custom orderby code.

cheers

#1842845

Hi Christian and Umberto, Barney asked me to have a look at this too. The custom code that addresses the need to sort on two custom fields replaces the meta_query part of the query so a normal Toolset filter that adds a meta_query to the query to filter on a custom field is going to be overwritten before the query runs. You can verify this with Query Monitor where you'll never see a query execute that has the filter criteria in it.

There are two paths to a solution to consider:

1) adjust the custom code so it detects if a meta_query is already in the query_args and, if yes, keeps it in place while adding the keys for the sorting instead of replacing it.

OR

2) intercept the URL parameters for the Toolset filter in the custom code and add one or both of the other thickness and width (the display versions, not the sort versions) to the meta_query if present in the parameters. These would need key and value, not just key and the compare would either be "= " for a single value or "IN" if checkboxes or multi-select allows multiple choices with the value formatted accordingly.

It'd be really helpful if Toolset someday makes an official way to have a primary and secondary sort that are both custom fields and that work properly with Toolset Filters but, for now, one of these kinds of workarounds is going to be needed.

#1843279

My issue is resolved now.

I followed Scott suggestion and I modified:

$query_args['meta_query'] = array(

To:

$query_args['meta_query']['1'] = array(

So now the filter is apending the new meta_query.

Thank you!

#1843429

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

I've updated the original thread with a solution that will continue to work regardless of how many custom query filters are applied on the page: https://toolset.com/forums/topic/how-do-i-display-a-search-result-first-on-col-a-then-col-b/#post-1843423

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.