Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Filter post titles in live search results
- This topic has 4 replies, 2 voices, and was last updated 3 years, 4 months ago by
thebizpixie.
-
AuthorPosts
-
January 23, 2023 at 6:58 am #41028
thebizpixie
ParticipantHi 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
January 23, 2023 at 9:52 am #41033Ernest Marcinko
KeymasterHi,
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_titlehook.Make sure you are using the the_title hook for modifying the title, it should apply for the live search results as well. Example:
add_filter( 'the_title', 'my_the_title', 10, 2 ); function my_the_title($title, $id) { return 'Custom Text: ' . $title; }January 23, 2023 at 11:40 pm #41066thebizpixie
ParticipantThanks 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 #41070Ernest Marcinko
KeymasterYou 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:
if ( !is_admin() || wp_doing_ajax() ) {January 29, 2023 at 10:43 pm #41147thebizpixie
ParticipantThanks, 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.