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>  
       </div>

    <?php endwhile; endif; ?>

    <?php wp_reset_postdata(); ?>
link|improve this question

70% accept rate
feedback

1 Answer

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',  ) );
link|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
feedback

Your Answer

 
or
required, but never shown

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