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?
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:
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.
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:
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.
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 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.
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.
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.