qtranslate / qtranslate-x / mqtranslate / qtranslate-slug : Fixing the „order by ‚title'“-problem

UPDATE: PLEASE LOOK AT THE BOTTOM FOR QTRANSLATE-X COMPATIBILITY:
Here we are again. When i built a two-lingual website for a customer, i ran into a problem with the sorting:

The customer needed the posts to be sorted by the title. Well, thats what the argument „orderby“ in WP_Query is good for, shouldn’t be a problem, right?

Wrong.

As we know, mqtranslate saves translated stuff like the post title like this in the database:
"<!--:de-->Beitragstitel<!--:--><!--:en-->Post title<!--:-->".
If we order by the Post title, the default language will be sorted correctly. The other language will be sorted the same. Meh.

I found no solution to this problem, so i made my own. If you need to sort by title and use mqtranslate, maybe this will help you too.

Open your themes functions.php and insert the following:

##UPDATE: UPDATED FUNCTION FOR QTRANSLATE-X AT THE BOTTOM ##

add_filter( 'posts_clauses', 'qtrans_fix_clauses', 999, 2 );
function qtrans_fix_clauses($clauses,$query){
if(!is_admin()){
global $q_config;
$lang = $q_config['language'];
if($query->query_vars['orderby'] == 'title'){
$clauses['orderby'] = "SUBSTR(post_title, LOCATE('<!--:".$lang."-->',post_title)+10,(LOCATE('<!--:-->',post_title,LOCATE('<!--:".$lang."-->',post_title)+10) - (LOCATE('<!--:".$lang."-->',post_title)+10)) ) ".$query->query_vars['order'];
}
}
return $clauses;
}

The filter „posts_clauses“ is called directly before the sql is sent to the database. We change the „order by“ clause, so that the query gets sorted by the part within the post_title that is between the active language tags.

UPDATE: IMPORTANT: If you get your posts via the „get_posts“ function, you need to pass the following argument within your args:
'suppress_filters' => false
as the get_posts function suppresses the posts_clauses filter by standard.

UPDATE 2 – QTRANSLATE-X:
As of now, the plugin mqtranslate is deprecated in favor of the new qtranslate-x. As this plugin can make use either of these language tags: <–:de–> Or these: [:de], we have to modify our code a little bit:

add_filter( 'posts_clauses', 'qtrans_fix_clauses', 999, 2 );
function qtrans_fix_clauses($clauses,$query){
if(!is_admin()){
global $q_config;
$lang = $q_config['language'];
if($query->query_vars['orderby'] == 'title'){
$clauses['orderby'] = "SUBSTR(post_title, IF(LOCATE('<!--:".$lang."-->',post_title),LOCATE('<!--:".$lang."-->',post_title)+10, LOCATE('[:".$lang."]',post_title)+5),
IF(LOCATE('<!--:".$lang."-->',post_title),LOCATE('<!--:-->',post_title,LOCATE('<!--:".$lang."-->',post_title)+10) - (LOCATE('<!--:".$lang."-->',post_title)+10),
LOCATE('[:',post_title,LOCATE('[:".$lang."]',post_title)+5) - (LOCATE('[:".$lang."]',post_title)+5))) ".$query->query_vars['order'];
}
}
return $clauses;
}

Now the function can sort both tags.

If you get problems with the code (or the code helped you), please leave a comment!

Happy Coding,
Kuchenundkakao

3 Gedanken zu „qtranslate / qtranslate-x / mqtranslate / qtranslate-slug : Fixing the „order by ‚title'“-problem

    • Hi Pedro! To be honest, i think this should be implemented in qtranslate-whatever, as it has nothing to do with the slugs.

  1. Pingback: caterhamcomputing on "[Plugin: CC Child Pages] Orderby title" * Best Wordpress Themes - Reviews

Schreibe einen Kommentar

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s