Filter post titles in live search results

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 thebizpixie 1 year, 2 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #41028
    thebizpixie
    thebizpixie
    Participant

    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.
    #41033
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    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:

    add_filter( 'the_title', 'my_the_title', 10, 2 );
    function my_the_title($title, $id) {
    	return 'Custom Text: ' . $title;
    }
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #41066
    thebizpixie
    thebizpixie
    Participant

    Thanks 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?

    #41070
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    You 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() ) {

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #41147
    thebizpixie
    thebizpixie
    Participant

    Thanks, 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.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.