Skip Navigation

[Resolved] Text Filter is “fuzzy” and not exact when used in views

This thread is resolved. Here is a description of the problem and solution.

Problem:
Search view with Exact title

Solution:
You can use the WordPress posts_search hook to alter the query to search by exact keyword/title.

You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/text-filter-is-fuzzy-and-not-exact-when-used-in-views/#post-1786111

Relevant Documentation:

This support ticket is created 3 years, 7 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 15 replies, has 2 voices.

Last updated by jacobB 3 years, 7 months ago.

Assisted by: Minesh.

Author
Posts
#1769779

I am trying to: use a filter to show details for a single entry on a page, using the documentation from here: https://toolset.com/documentation/user-guides/views/filtering-views-for-a-specific-text-string-search/#search-for-a-text-set-by-the-view-shortcode-attribute

Link to a page where the issue can be seen: hidden link

I expected to see: information about "Weller Garden" which is located in New York State.

Instead, I got: information about "George Sweller Daylily Display Garden" in Moscow, ID. You can see that every detail aside from the title of the page is identical between the two: hidden link and hidden link

The shortcode that I'm using for the Weller garden is: [wpv-view name="single-display-garden-info" searchname="Weller Garden"]
The shortcode that I'm using for the George Sweller garden is: [wpv-view name="single-display-garden-info" searchname="George Sweller Daylily Display Garden"]

This is the first time that I've encountered a problem, because all of my other entries are apparently more unique. In case it helps, the George Sweller entry was created a while ago, and the Weller Garden entry was only added in the last couple of weeks.

To me, it seems like the text filter is doing a fuzzy search and finding the closest match instead of doing an exact match. Does that sound close to how it's designed? I'm definitely looking for a more direct match.

Thank you!

#1770295

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

As I understand, you want to filter the results with the exact string match correct? If yes:

To filter the view with exact string match, you will require to add filter "posts_search". You can find the related solution with the following reply.
=> https://toolset.com/forums/topic/keyword-search-needs-to-be-absolute/#post-1119036

You may require to adjust the filter code I shared as per your requirement. If you do not know how to do it, please send me problem URL and access details, I would be happy to adjust it on your behalf.

I have set the next reply to private which means only you and I have access to it.

#1771009

Good Morning,

Thank you for your help so far. You are correct - I'm trying to do an exact match on the string instead of an approximate or fuzzy match.

I have copied and pasted the code into the Functions.php file for my current theme, and I changed the view ID to match the one that I'm trying to adjust. However, this hasn't corrected the issue.

#1771071

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please share problem URL where I can see the search form as well as access details so I can see whats going wrong there.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1771589

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Thank you for the access details.

I see how you configured your view. I see you added the view as given under to every post of your post type "Display Gardens".

[wpv-view name="single-display-garden-info" searchname="Weller Garden"]

- As I understand, you want to filter the view results with your post title. You can even filter with post ID.

Rather editing every post, why dont you either create content template or Layout and assign that content template or layout to display with the post type "Display Gardens". The benefit of this will be you will not be require to edit the each post.

Once you confirm what approach you would like to go with, I will address the exact search string issue after that. As if you want to filter the post with exact match (post title), then we can even filter with the post ID as well.

#1775991

I'm still trying to sort through your last message. I believe I tried to use the content templates and layouts first, but my theme (which was custom built by someone other than me) did not show any of the details. The theme is a disaster...

I'm working on getting a copy of the site moved to a dedicated Development environment so that I can feel more confident in making this type of change.

#1776695

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok great - Please let me know if you will require further assistance.

#1784591

Hello,

I have copied my site to a new development environment. I attempted to create a new Layout for the content type Display Garden, and I applied it to all of the posts of that type. The problem that I have with my theme is that no matter what I put into the body of the layout, the only thing I get to display on the site is just the title of the post. Even if I assign static text to the layout, it does not get displayed by my site's theme.

When I created the content type "Display Garden", I used the existing single post template file (single.php) and made a copy of it called single-display-garden.php. I only changed a minor portion of the code within that file, and it was only to tie the theme files to the content type.

Here's the code in that PHP file:

<?php
class single_display_garden extends Core_Template {
  public function post_data()
  {
    global $post;
    return [
      'title' => $post->post_title,
      'content' =>  apply_filters('the_content', $post->post_content),
    ];
  }
}
global $post;
new Single_Display_Garden($post->ID);

For whatever reason, the toolset layouts never get passed in as "the_content" no matter what I've tried.

Any ideas to overcome this obstacle so that we can get back to the original question? 🙂

#1785391

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

To display the layout content, you will require to use the function:

the_ddlayout();

Please check the following Doc for more info:
=> https://toolset.com/documentation/user-guides/layouts/develop-layouts-based-themes/#php-integration-for-layouts

#1786053

That doesn't quite work. My theme file doesn't contain the while loop that is mentioned in the documentation. I've added it into the code where it seems to fit, but now the content is appearing above the title and behind the main menu/site hero, which is not useful.

I'm using Timber and Twigs for the theme on the site, if that helps. I've searched for ways to use the the_ddlayout() function within Timber/Twigs, but I haven't found anything helpful.

#1786055

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I'm afraid that theme customization work is beyond the scope of our support policy.

You may contact your theme author and check with them how you can integrate layouts with it as I already shared the doc with you:
=> https://toolset.com/documentation/user-guides/layouts/develop-layouts-based-themes/#php-integration-for-layouts

#1786057

Ok, then back to the original problem: how do I get it to do an exact search instead of the fuzzy search without moving to a Toolset Layout for the content type?

#1786111

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok

I've added the following filter to "Custom Code" section offered by Toolset:
=> hidden link

function func_search_by_exact_title( $search, $wp_query ){
global $wpdb;
global $WP_Views;
  
if($WP_Views->current_view == 9952){
  
  			$search = $WP_Views->view_shortcode_attributes[0]['searchname'];
            if ( empty( $search ) )
                    return $search; // skip processing - no search term in query
  
                    $term = esc_sql( like_escape( $search ) );
                    $search = " AND ($wpdb->posts.post_title REGEXP '[[:<:]]{$term}[[:>:]]')";
  }
return $search;
}
add_filter( 'posts_search', 'func_search_by_exact_title', 1000, 2 );

Can you please confirm it works as expected.

#1786117

Yes - that appears to work exactly as expected. Thank you!

Is this a permanent change, or will I need to incorporate that code into some other file?

#1786135

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

No need to add that code to other field and the code I've added will only apply to view ID "9952" - so if you have other views those will not be get affected.

Glad to know that solution I shared help you to resolve your issue.

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