Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've got a pretty simple WP installation, where I use qtranslate plugin to keep site in two languages. The problem is, I need to sort posts by title, and as probably you know (as you use qtranslate), that causes the problem - it sorts posts by mysql entries (which are two because of languages), and that gives me unwanted order (from the first language).

What I want to do, is get the first letter of evert post and sort it by this, in php, do you think it is good solution? If so, how to do this best way?

<?php query_posts( array ( 'category_name' => 'publishers', 'orderby' => 'title', 'order' => 'ASC' ) ); ?>
share|improve this question
    
I'd recommend using WP_Query instead of query_posts. –  helgatheviking Nov 17 '13 at 15:59

1 Answer 1

up vote 1 down vote accepted

I'm afraid it would be very hard to do. qTranslate stores all titles in one database field as concatenated string with some additional markup, so there is no chance to sort them using SQL and to do it fast.

You can do one of 2 things:

1. Stop using qTranslate

It really has many other flaws. You can use WPML or Polylang instead - both are much better.

2. Sort them in PHP

Don't sort posts in query with SQL and sort them with PHP after running this query. WP_Query stores found posts in $wp_query->posts. You can sort this array with PHP. Of course you will have to select all posts to make it work, so if there are many posts in this query, it's rather bad idea. But if there are only few of them, then it shouldn't be so bad...

Sample code below - written in here, so may be buggy.

<?php
    $my_query = new WP_Query( array('post_type'=>'page', 'orderby'=>'title', 'order'=>'ASC', 'posts_per_page'=>-1) );
    if ( $my_query ->have_posts() ):
?>
<ul class="posts-list">
    <?php
        $found_posts = array();
        foreach ( $my_query->posts as $k=>$p ) {
            $found_posts[ apply_filters('the_title', $p->post_title) ] = $p;
        }
        ksort($found_posts);
        $i=0;
        foreach ($found_posts as $k=>$p) {
            $my_query->posts[$i++] = $p;
        }
    ?>
    <?php while ( $my_query->have_posts() ): $my_query->the_post(); ?>
        <li><h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2></li>
    <?php endwhile; ?>
</ul>
<?php endif; wp_reset_postdata(); ?>
share|improve this answer
    
Hi, I see we are both from Poland :). So Polylang can do this? I've got about 20 posts… And was wondering to do this even by js, by php seems better –  user1854236 Nov 17 '13 at 12:01
    
Polylang (like WPML) stores every version of post as separate post, so you can sort them easily. If you can change plugin now, I would change it ASAP. If you can't (site is big and it would be time-consuming) you can always try to sort them in PHP. –  Krzysiek Dróżdż Nov 17 '13 at 12:21
    
Could you post me some example how to sort inside query in php? It will be very helpfull –  user1854236 Nov 17 '13 at 12:23
    
Just to add my 2 off-topic cents, I've found WPML much more confusing for admins, much more cluttered, buggier and and much more prone to deleting translations by mistake. Qtranslate is clunky/hacky, but works and is much more usable for most use cases. –  guidod Apr 25 at 18:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.