I am using the parametric search with one of my custom post types. I was under the impression that the wpv-filter-search-box used the standard WordPress search, but I must be wrong. I would like to use this field to search the post body plus a custom field. I have installed Relevanssi and it is working fine with the standard WP search box, but it does not seem to have an effect on the wpv-filter-search-box. If the two are not compatible, is there a way to supply the WP/Relevanssi search results to the parametric search script so that it can further filter the search results using the lovely auto-generated form fields that come with parametric search?
Although it's correct that wpv-filter-search-box tries to take the advantage of WP search standards, but it also does some own things to return the results. On the other hand, it uses some hooks which might be too early (or may overwrite) the hooks defined by the Relevanssi.
However, you can alter View's main query by using Views API. wpv_filter_query() offers a way to alter the query, so you can add/remove additional query parameters.
Thanks for pointing that out - I think it will be useful in the future. But for this case, I'm not seeing any way to alter the query here. I want to retrieve posts that contain the search term in the post body OR in a custom title field. This would then be filtered by other meta fields using AND.
Without Views, I could run two queries and merge them (or just use SQL), but I don't think that is an option here. It looks like I need to create my own search/filter form.
Actually when you use Views search form and insert the short code in the views, it asks you to search for "just post title" or "content and title" (please see attached). So your half of the work is done.
For making it to look for the same search term in a certain custom field, you can add following code to your functions.php file:
add_filter( 'wpv_filter_query', 'extend_search_box', 99, 3 );
function extend_search_box( $query_args, $view_settings, $view_id ) {
if($view_id == 1234) { // alter query for a particular view
$search_string = $_REQUEST["s"];
// remember to change "s" with appropriate name of URL param, being used to hold the search string.
$query_args['meta_key'] = 'my-custom-field'; // change custom field name to the right one
$query_args['meta_value'] = $search_string;
$query_args['meta_compare'] = 'LIKE';
}
return $query_args;
}
So when a search term is queried using View's search box, it will additionally try to match the content within the custom field 'my-custom-field'.
[s] => test
[meta_key] => wpcf-article-title
[meta_value] => test
[meta_compare] => LIKE
)
FYI, I have a test post with the term 'test' in the body, title, and my custom field. The parametric search form successfully finds the post when I have the code for the meta field commented out. When the meta field is added to the query args, I get 'No items found'.
Can I ask for a temporary access to your site? So I can look for more details.
I have enabled your next reply as private, please input all details in that area. Please mention the links to the pages, views, forms, CPTs and configurations in question.
Please take a backup of your site, before proceeding.
I have tried the wpv_filter_query() with your view, and that seems to take effect, BUT, of course not as intended. As a matter of fact, when a meta_query is added, Word Press' WP_Query() uses an 'AND' operator to combine with default post_title and post_content filters. Which ultimately requires the search term to exist in all the matching fields.
This is a native behaviour of Word Press and requires a lot of code implementation to change to an 'OR' operator. As mentioned here: http://wordpress.stackexchange.com/a/99861 But this very solution isn't compatible with Views own query progress, since it uses entirely a new query object. Which overrides (or eliminates) other filters in this view's configuration.
In short, and unfortunately, this isn't possible with the Views at the moment. I will suggest to rely on a 3rd party plugin for this purpose, or to go with a custom solution. You can also contact our Certified Partners at https://toolset.com/consultant/ to have them build a custom solution based on Toolset.
I have forwarded this as a feature suggestion to our Dev Team also.
Our team suggests a work around, please consider following code:
add_filter( 'wpv_filter_query', 'hook_search_custom_fields', 10, 2 );
function hook_search_custom_fields( $query, $view_settings ) {
if ( $view_settings['view_id'] == 1234 ) {
add_filter( 'posts_search', 'search_custom_fields', 600, 2 );
}
return $query;
}
function search_custom_fields( $search, &$wp_query ) {
global $wpdb;
if (isset($_GET['wpv_post_search'])) {
$s = esc_attr($_GET['wpv_post_search']);
$search = substr(trim($search), 0, -2) . " OR (wp_posts.ID IN (SELECT post_id FROM $wpdb->postmeta WHERE meta_key in ('wpcf-field1', 'wpcf-field2', 'wpcf-field3') AND meta_value LIKE '%$s%'))))";
}
remove_filter( 'posts_search', 'search_custom_fields', 600, 2 );
return $search;
}
Please remember to make following adjustments in above code:
- change 1234 to proper view ID in hook_search_custom_fields() function
- change 'wpcf-field1', 'wpcf-field2', 'wpcf-field3' to appropriate custom field names in search_custom_fields() function.