Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Filter post titles in live search results
This topic contains 4 replies, has 2 voices, and was last updated by thebizpixie 1 year, 2 months ago.
- AuthorPosts
- January 23, 2023 at 6:58 am #41028
Hi Ernest,
Is there a way to filter the titles that are used in the live search? Here’s my use case.
I have a CPT of “resource” that I’ve added to my search results, and only in certain contexts, I filter the title to add the format of the resource as a prefix to the title e.g. Video: Ways to Reduce Staff Turnover: Solutions, Strategies and Methods
But the actual title value of the post in WordPress does not have the prefix, because that’s the main title, and I want to add/update the prefix programmatically.
However, in the live search results (ie when I’m typing into the search box), it’s showing up without the prefix, because the Index Table is using the original post title.
This causes confusion sometimes because we have resources and articles with very similar titles and it’s hard to tell them apart in the live search results (the article and the resource are related, but are in different formats and have slightly different information).
So what I’d like is some way to filter the title that used in the live search results, if possible. Any suggestions?
Thanks,
Nikki
Attachments:
You must be logged in to view attached files.January 23, 2023 at 9:52 am #41033Hi,
The plugin uses the get_the_title() function to request the title, regarless of the engine used (both index and regular). That function automatically applies the
the_title
hook.Make sure you are using the the_title hook for modifying the title, it should apply for the live search results as well. Example:
Best,add_filter( 'the_title', 'my_the_title', 10, 2 ); function my_the_title($title, $id) { return 'Custom Text: ' . $title; }
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
January 23, 2023 at 11:40 pm #41066Thanks for confirming how it’s supposed to work. I’m definitely using the ‘the_title’ filter to modify the title:
if( !is_admin() ){ add_filter( 'the_title', 'nhs_skhc_modify_resource_title', 10, 2 ); }
and yet it’s not applying to the live search results titles.
Do you need me to share anything more with you to figure out why this is not working as expected?
January 24, 2023 at 1:46 pm #41070You should try without the
!is_admin()
criteria. For ajax requests is_admin() is true, as it acts as a query to the administrator interface.Alternatively:
Best,if ( !is_admin() || wp_doing_ajax() ) {
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
January 29, 2023 at 10:43 pm #41147Thanks, the “wp_doing_ajax()” fixed it.
I need the filter off in admin because it’s not technically part of the title for people who are editing the resources.
All working as expected now.
Cheers.
- AuthorPosts
You must be logged in to reply to this topic.