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'm having a problem with entries for a custom post type not being ordered as expected inside a custom query:

<h4>Related projects</h4>

            <?php

                $rp_args = array(
                        'post_type' => 'work',
                        'post_status' => 'publish',
                        'orderby' => 'title',
                        'order' => 'ASC',
                        );


                $related_projects = new WP_Query($rp_args);
            ?>

            <ul>
                <?php 
                if($related_projects->have_posts()):
                while ($related_projects->have_posts()) : $related_projects->the_post();
            ?>
                <li><a href="<?php the_permalink();?>"><?php the_title();?></a></li>
            <?php
               endwhile;
               endif;
            ?>
            </ul>

It's supposed to just simply pull out a custom post type and order it by name - but the ordering isn't working and I'm not sure why not.

It's simply listing the posts in the order they appear in the WP admin.

Any thoughts on why my ordering isn't kicking in? It finds the correct post type and number of posts I specify - just the ordering doesn't seem to work.

share|improve this question
    
@Simon - Do you have any plugins that might be modifying the order queries? That would be the first place I'd look... Please update your question to list the plugins you are using. –  MikeSchinkel Feb 3 '11 at 13:20
    
Paste some of the output you get,so we can see what you are getting. –  Bainternet Feb 3 '11 at 13:20
    
@Simon Also add this right after your new WP_Query(...) line to see what the SQL query looks like, and post that: echo $related_projects->request; exit; –  MikeSchinkel Feb 3 '11 at 13:22
2  
I'd suggest avoiding interferance from plugins by passing in the suppress_filters parameter. Eg. suppress_filters => true this will bypass any filters plugins are performing on the query... ;) –  t31os Feb 3 '11 at 13:32
    
I may be wrong, but isn't get_posts() preferred for custom loops? –  tnorthcutt Feb 3 '11 at 14:03

2 Answers 2

Yep - I'm stupid.

Post Types Order plugin has an option (set by default) which overrides any ordering done in queries. Turn that off and the problem goes away.

share|improve this answer
1  
Can you please elaborate on exactly what you fixed so that others with the same problem can benefit? –  MikeSchinkel Feb 3 '11 at 14:09
    
Wow, this just saved me a long time (well I already spent a long time..). Here's a screenshot of what to uncheck in Settings > Post type order: i.imgur.com/lKWwXQq.png –  notlaura Jun 27 at 19:48

into the file category.php use for example

sort_query_posts_by('title', 'desc');

or use ID, date etc. (see the function below) before loop and insert the function below

if (! function_exists('sort_query_posts_by'))
{
    function sort_query_posts_by($order_by, $order = 'asc')
    {
        global $wp_query;
        $order_by = strtolower($order_by);
        $order    = strtolower($order);

        if ($order_by == 'rand') {
            shuffle($wp_query->posts);
            return;
        }

        if ($order_by == 'none') {
            $order_by = 'id';
            $order = 'asc';
        }

        $props = array(
            'author'        => 'return sqp_compare_by_number($o1->post_author, $o2->post_author, '.$order.');',
            'comment_count' => 'return sqp_compare_by_number($o1->comment_count, $o2->comment_count, '.$order.');',
            'date'          => 'return sqp_compare_by_number(strtotime($o1->post_date), strtotime($o2->post_date), '.$order.');',
            'id'            => 'return sqp_compare_by_number($o1->ID, $o2->ID, '.$order.');',
            'menu_order'    => 'return sqp_compare_by_number($o1->menu_order, $o2->menu_order, '.$order.');',
            'modified'      => 'return sqp_compare_by_number(strtotime($o1->post_modified), strtotime($o2->post_modified), '.$order.');',
            'parent'        => 'return sqp_compare_by_number($o1->post_parent, $o2->post_parent, '.$order.');',
            'title'         => 'return sqp_compare_by_string($o1->post_title, $o2->post_title, '.$order.');'
        );

        usort($wp_query->posts, create_function('$o1, $o2', $props[$order_by]));
    }

    function sqp_compare_by_number($n1, $n2, $order)
    {
        $n1 = (int) $n1;
        $n2 = (int) $n2;
        $v  = $n1 > $n2 ? 1 : ($n1 < $n2 ? -1 : 0);
        return ($order == 'desc') ? $v * -1 : $v;
    }

    function sqp_compare_by_string($s1, $s2, $order)
    {
        $v = strnatcasecmp($s1, $s2);
        return ($order == 'desc') ? $v * -1 : $v;
    }
}
share|improve this answer

Your Answer

 
discard

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