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 currently have the following custom wp_query which displays posts from 2 custom WP roles, "custom_role_one" and "custom_role_two". It displays the posts from my custom post type of "listing". It works great but how can I modify this so that the posts are ordered by each role?

For example, I would like all posts from "custom_role_one" to be displayed first and then all posts from "custom_role_two" to be listed second. As you can see, they are currently ordered by date.

<?php $custom_roles = get_users( array( 'role' => 'custom_role_one, custom_role_two' ) );

        $custom_ids = array();

        foreach( $custom_roles as $custom_role ) 
            $custom_ids[] = $custom_role->ID;

        $custom_role_query = new WP_Query( array( 'author' => implode( ',', $custom_ids ), 'post_type' => 'listing', 'posts_per_page' => 10, 'orderby' => 'date', 'order' => 'DESC',  'paged' => get_query_var('paged') ) );

    <?php if ( $custom_role_query ->have_posts() ) : while ( $custom_role_query ->have_posts() ) : $custom_role_query ->the_post(); ?>  

       <div id="post-<?php the_ID(); ?>">
            <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>  

    <?php endwhile; endif; ?>

    <?php wp_reset_postdata(); ?>
share|improve this question
up vote 2 down vote accepted

you can add a custom field to each listing post with the user's role and then in your query you can order by that field, for example say you have a custom field named `u_role' then your query should look like this:

$custom_role_query = new WP_Query( array(
'author' => implode( ',', $custom_ids ), 
'post_type' => 'listing', 
'posts_per_page' => 10, 
'paged' => get_query_var('paged'),
'meta_key' => 'u_role',
'orderby' => 'meta_value', 
'order' => 'DESC',  ) );
share|improve this answer
Thanks, I'll give this a shot. Could I dynamically populate the custom field and also make it hidden from view? – Andrew Jun 24 '11 at 4:29
Thanks, this is resolved. I used WP Alchemy for the metabox creation on each post with a dynamic value that pulled in the authors custom WP role. I then used the meta_key and meta_value to sort them. – Andrew Jun 26 '11 at 5:02

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.