Sort order in get_posts

I've created a custom template for a client. This is going to be a multilingual site setup on a Multisite install.

As you can see under each category letter, the posts start out in alphabetical order but "M" and "O" are all out of whack.

If you click through onto those categories, using the alphabet bar at the top, those posts are in alphabetical order.

Both pages are using the same code, so I've no idea what I'm doing wrong. I have a feeling I have a query conflict somewhere but I can't find it. Here's the code that does the sorting:

section class="atozlist"
    ?php
        $newargs = array(
            'child_of'  = $catID,
            'order'     = 'ASC'
            );
        $categories =  get_categories($newargs);
        foreach  ($categories as $category) {
            echo 'h2'.$category-name.'/h2';
            echo 'ul';

            $titleArgs = array(
                'cat'       = $category-term_id,
                'order_by'  = 'title',
                'order'     = 'ASC'
                );
            foreach (get_posts($titleArgs) as $post) {
                setup_postdata( $post );
                echo 'lia href="'.get_permalink($post-ID).'"'.get_the_title().'/a/li'; 
            } 
            wp_reset_postdata();
            echo '/ul';
        }
    ?
/section

I'm unsure about the implementation of setup_postdata($post), though I used this as a template. I'm also unsure if I've implemented wp_reset_postdata() correctly.

Anyone any ideas?

Topic wp-reset-postdata get-posts query Wordpress

Category Web


Ah yeah... we all missed the order_by, didn't we... Should be orderby.

Dammit.

Thanks for the help everyone anyway.


The problem here is not that the posts that are being wrongly displayed are displayed under the wrong category, it's that they're not actually in the right category. For example, the post "Supporting healthy eating choices" is displayed under "O" as it has the category "O". Going to your backend and assigning the proper category to these posts should fix it. In short, there's nothing wrong with your code, some of your posts just belong to the wrong categories.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.