Skip Navigation

[Resolved] Can Relevanssi be used to expand the capability of the wpv-filter-search-box?

This support ticket is created 7 years, 10 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
- 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 -
- - - - - - -

Supporter timezone: Asia/Karachi (GMT+05:00)

Tagged: 

This topic contains 11 replies, has 2 voices.

Last updated by laurieL-2 7 years, 10 months ago.

Assigned support staff: Waqas.

Author
Posts
#318720

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?

#318781

Waqas
Supporter

Languages: English (English )

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

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.

Please see https://toolset.com/documentation/user-guides/views-filters/wpv_filter_query/ for more information.

Please let me know if I can help you with anything related.

#318993

Hi Waqas,

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.

Please let me know if you have other ideas.

Thanks.

#319070

Waqas
Supporter

Languages: English (English )

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

views - content search box.png

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'.

Please see http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters for more information on Custom Fields Parameters used by WP_Query() for this purpose.

#319399

Hi Waqas,

Thanks for the code - it really looks like it should work, but I'm still having a problem. Here is what is in my $query_args ...

Array
(
[posts_per_page] => 20
[paged] => 1
[post_type] => Array
(
[0] => article
)

[order] => DESC
[suppress_filters] =>
[ignore_sticky_posts] => 1
[post__not_in] => Array
(
[0] => 5304222
)

[wpv_original_limit] => -1
[wpv_original_offset] => 0
[wpv_original_posts_per_page] => 20
[post_status] => Array
(
[0] => publish
[1] => private
)

[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'.

Thanks for your continued help.

#319404

I also wanted to add that I have tried the parametric search with all plugins disabled except Types and Views.

#319497

Waqas
Supporter

Languages: English (English )

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

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.

#320363

Waqas
Supporter

Languages: English (English )

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

Thank you for providing the details. Please allow me some time to work on this, I will update you as soon as I find a solution.

#320761

Waqas
Supporter

Languages: English (English )

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

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.

#321145

Waqas
Supporter

Languages: English (English )

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

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.

Please let me know if it helps, thanks.

#321417

Thank you very much for your persistence. This looks like what I need.

#321763

Just an additional note for anyone else who might copy this code:

- change 'wp_posts.ID' to match the name of your posts table