Documentation > Developers > Implementing a custom search solution

Implementing a custom search solution

Browse Docs
Search Documentation

If you are looking for search solution for your content, you need to know that any plugin that is doing FILTERING based on taxonomy or custom fields will work.
You can use WP_Query with the query[‘s’] parameter and it will work out-of-the-box to provide relevant posts for that key term.
The key term needs to be in same language as the current language being displayed when loading that page. In code, posts returned will be in the default language (IDs, post title, post content etc.) but the content echoed is translated on a later hook, so it will be displayed in the translated language in front-end.

Only post title and post content are searchable in translated languages, external custom fields such as ACF are not. Depending on your setup, if you can include custom fields content inside the_content hook, then the custom fields are searchable as well.
For the search to work on a specific post in a specific language, translation of that post content and title in that same language must exist prior to searching. If automatic translation is active, a post must be visited at least once in that specific language in order to get indexed by TP.
However, any plugin that’s using an index, will not work correctly with our plugin.

If you are looking to make TranslatePress compatible with a custom search functionality where it doesn’t work out of the box, here are some things you can should know about.

TranslatePress hooks a function to the filter ‘pre_get_posts’ in order to adjust the query for the search functionality in a translated language.
If the query[‘s’] is set and the currently viewed page is in a translated language, then TP performs a search and returns the post ids of the default language where that key term is found in the equivalent translated posts.

There is a filter you can use in order to apply this hook regardless of the status for: !is_admin() && $query->is_main_query() && $query->is_search().
The TranslatePress code looks like this:
if ( ( !is_admin() && $query->is_main_query() && $query->is_search() ) || apply_filters( 'trp_force_search', false ) )
So you can hook to ‘trp_force_search’ to enable searching in different conditions. To enable search all the time use the code:
add_filter('trp_force_search', '__return_true');

Here is an example on how to create compatibility with a search functionality that is not using the WordPress default search system through WP_Query. In the following code, the function returns the post ids for a particular search key in a translated language.

add_filter( 'aws_search_results_products_ids', 'trp_aws_search_results_products_ids', 10, 2 );
function trp_aws_search_results_products_ids( $posts_ids, $s ){
global $TRP_LANGUAGE;
$trp = TRP_Translate_Press::get_trp_instance();
$trp_settings = $trp->get_component( 'settings' );
$settings = $trp_settings->get_settings();

if ( $TRP_LANGUAGE !== $settings[‘default-language’] ) {
$trp_search = $trp->get_component( ‘search’ );
$search_result_ids = $trp_search->get_post_ids_containing_search_term($s, null);

if (!empty ( $search_result_ids) ) {
return $search_result_ids;
}
}

return $posts_ids;
}

TranslatePress Multilingual

The best website localization service for WordPress websites. Manage everything from a visual interface.