Adding ZdMultilang tables to WordPress search feature
-
Hello All,
This is my first post to this forum. I always get a lot of help from here and I hope someone can help me with this issue.
I′m setting up a bilingual blog using the amazing ZdMultilanguage plugin. It′s a really good plugin but, as far as I can tell, it doesn′t search the translations for the posts. I tested it a lot and I came across a post in the codex about adding the plugin tables to the posts search in the functions.php. https://codex.www.ads-software.com/Custom_Queries#Keyword_Search_in_Plugin_Table
I used the code there, but no luck, I can′t search in the second language unless I switch them in the backend config and the second language becomes the first language.
Here is my code:
add_filter('posts_join', 'mlang_search_join' ); add_filter('posts_where', 'mlang_search_where' ); add_filter('posts_groupby', 'mlang_search_groupby' ); function mlang_search_join($join) { global $zd_ml_trans, $wpdb; if(is_search()) { $join .= " LEFT JOIN $zd_ml_trans ON " . $wpdb->posts . ".ID = " . $zd_ml_trans . ".post_content "; } return $join; } function multilang_search_where($where) { if( is_search() ) { $where = preg_replace( "/\(\s*post_title\s+LIKE\s*(\'[^\']+\')\s*\)/", "(post_title LIKE \\1) OR (post_content LIKE \\1) OR (post_excerpt LIKE \\1) OR (post_status LIKE \\1)", $where ); } return $where; } function multilang_search_groupby( $groupby ) { global $wpdb; if( !is_search() ) { return $groupby; } // we need to group on post ID $mygroupby = "{$wpdb->posts}.ID"; if( preg_match( "/$mygroupby/", $groupby )) { // grouping we need is already there return $groupby; } if( !strlen(trim($groupby))) { // groupby was empty, use ours return $mygroupby; } // wasn't empty, append ours return $groupby . ", " . $mygroupby; } } ?>
I would really appreciate any help and thank you all in advance for your attention.
Best Regards,
Tiago Morena
- The topic ‘Adding ZdMultilang tables to WordPress search feature’ is closed to new replies.