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 using the following code from this answer: How can I get the last posts by user role ?

<?php
$friends = get_users( array( 'role' => 'friends' ) );
$friend_ids = array();

foreach( $friends as $friend ) 
    $friend_ids[] = $friend->ID;

$news = new WP_Query( array( 'author' => implode( ',', $friend_ids ), 'post_type' => 'news', 'paged' => get_query_var('paged') ) ); ?>

I've added another WP role to the array so it grabs both. For example:

$friends = get_users( array( 'role' => 'friends, enemies' ) );

Is it possible to now order the posts somehow by the role? so it displays all the friends first, followed by all the enemies etc. At the moment, it lists them all mumbo jumbo as expected.

EDIT: sorry, probably shouldn't have set up another question, not sure why I did that. Is it possible to do this without using custom fields?

The problem I've found with custom fields is that If a user changed roles (due to membership levels) then the posts would not be ordered correctly until the post (from the author who changed) was saved with it's new custom field value.

share|improve this question
    
'role' doesn't seem to support multiple values. You could have called get_users for each role alphabetically then merge the resulting arrays. –  Natebot May 30 '12 at 1:05
add comment

1 Answer

Its possible with an ugly custom query but to save your self a lot of time an easier solution would be to add a custom field to each post with the user role , then your query would be much smoother for example say you have a custom field named `u_role' then your query should look like this:

$news = new WP_Query( array(
    'author' => implode( ',', $friend_ids ),
    'post_type' => 'news', 
    'paged' => get_query_var('paged'),
    'meta_key' => 'u_role',
    'orderby' => 'meta_value', 
    'order' => 'DESC') ); 
share|improve this answer
    
Thanks @bainternet. You actually provided this answer in my other question and I did do it this and it works, but was going to have another stab at this one. I guess there's no other way then. The problem with custom fields as I found out was that If a user changed roles (due to membership levels) then the posts would not be ordered correctly until the post was saved with it's new custom field value. –  Andrew Jun 27 '11 at 12:21
    
that really depends on how you are saving the custom field. For example if you save it on post_published hook then that wont change even if the user changes his role. –  Bainternet Jun 27 '11 at 12:26
    
In this particular instance, the custom field somehow needs to know when the author's role has changed. Is this possible? I'm using WP Alchemy to create the custom field, which I put a "1" or "2" depending on their role. Then the posts on the front end list all the 2's first, then the 1's. The posts will get a bit muddled up after a while if authors change their role though. –  Andrew Jun 27 '11 at 12:32
add comment

Your Answer

 
discard

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.