WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using the technique described here Seperating Custom Post Search Results and here posts_groupby problem to seperate my search results and group them by post type. This means I have the following code.

In functions.php

add_filter('posts_orderby', 'group_by_post_type', 10, 2);
function group_by_post_type($orderby, $query) {
    global $wpdb;
    if ($query->is_search) {
        return $wpdb->posts . '.post_type DESC';
    // provide a default fallback return if the above condition is not true
    return $orderby;

In search.php

<?php $last_type="";
$typecount = 0;
while (have_posts()){
    if ($last_type != $post->post_type){
        $typecount = $typecount + 1;
        if ($typecount > 1){
            echo '</ol>'; //close type container
        // save the post type.
        $last_type = $post->post_type;
        //open type container
        switch ($post->post_type) {
            case 'post':
                echo "<h2>News Articles</h2><ol>";
            case 'page':
                echo "<h2>Pages</h2><ol>";
            case 'work':
                echo "<h2>Projects</h2><ol>";
            case 'bootlegs':
                echo "<h2>Bootlegs</h2><ol>";
            case 'directors':
                echo "<h2>Directors</h2><ol>";
            //add as many as you need.
    } ?>

This works perfectly except I have no control over the order in which the Custom Post Types are displayed. I'm guessing they are being ordered by an internal ID number, but I need to have control over them.

How can I achieve this?


share|improve this question
up vote 1 down vote accepted

What kind of control do you want exactly? There are, for example, plugins that provide a UI through which you can order posts by various parameters.

If you just want to order them all by one parameter that will not change, you can simply add it to your query (since you can order by multiple conditions):

if ($query->is_search) {
    return $wpdb->posts . '.post_type DESC, title DESC';

Edit: here's another question you might find useful.

share|improve this answer
Thanks for your response. I want to order them manually for what is best suited to the project. In this case: Directors, Projects, Bootlegs, Pages, News Articles. Is this possible? Thanks in advance! – Jacob Apr 7 '13 at 20:00

In this case the fallback (secondary) order is the title.

In functions.php:

add_filter( 'pre_get_posts', 'ordering_post_title_type');
function ordering_post_title( $query ) {

    if ( $query->is_search ) {
        $query->set('orderby','type title');

    return $query;

share|improve this answer

Your Answer


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.