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 want to query posts by their author role. And do something with the post based on the role.

I know we can get the post by get_posts or WP_query, the problem is there are no argument to sort the post based on the author role. Or, we also can combine get_users and get_posts together, like this

$users = get_users(array(role => 'author'));
foreach($users as $user){
//here we can use get_posts to query the posts by the $user->ID   
} .....

this is too clumsy to doing this. I want to know if there any other way to query the posts based on the role, SQL query perhaps?

share|improve this question

3 Answers 3

up vote -3 down vote accepted
+50

I haven't really messed around with custom post queries before, but here is my try at a solution:

function get_posts_by_author_role($role) {
    global $wpdb;
    return $wpdb->get_results( "SELECT p.* FROM {$wpdb->posts} p, {$wpdb->usermeta} u"
                                ." WHERE    p.post_type     = 'post'"
                                ." AND      p.post_status   = 'publish'"
                                ." AND      u.user_id       = p.`post_author`"
                                ." AND      u.meta_key      = 'wp_capabilities'"
                                ." AND      u.meta_value    LIKE '%\"{$role}\"%'" );
}

This function will return posts only if their author has the role specified. It is tested and working on my local install of 3.4, but let me know if you are having any problems with it.

I hope this helps.

Example Usage:

$posts = get_posts_by_author_role('author');
foreach($posts as $post) echo $post->post_title, '<br />';
share|improve this answer

try this

create a function to alter the query's where clause:

function authors_where_filter( $where ) {
        global $wpdb;
        $ids = get_users(array('role' => 'author' ,'fields' => 'ID'));
        $where .= " AND post_author IN ($ids)";
        return $where;
}

and then before you query just hook it ex:

add_filter('posts_where','authors_where_filter');
$all_posts = new WP_Query(array('posts_per_page' => -1 .....
remove_filter('posts_where');

and you should get all posts of author users in a single query, (well two actually one to get the users and the other is to get the posts)

share|improve this answer

You can use Roles and Capabilitied check by deploying function:

current_user_can( $capability );

For example:

if(current_user_can('read') && !current_user_can('edit_posts')){
//code for subscribers...
}
else if(current_user_can('edit_posts') && !current_user_can('edit_pages')){
//code for authors...
}
else if(current_user_can('edit_pages') && !current_user_can('delete_themes')){
//code for editors....
}
else {
//code for admins...
}

You will have one more check if you use multi-site for super admins.

Note that you have to use two checks because without other check, some user would get multiple queryes.

share|improve this answer
    
the problem is the function is made to run in behind, not depend to current logged in user. And I prefer to use the role's name to run the check,because I have multiple custom user role which created by role management plugin. –  dev-jim Jun 21 '12 at 16:24
    
Maybe you can use this function then that will return user role as a string, and then check it with if's function get_current_user_role() { global $wp_roles; $current_user = wp_get_current_user(); $roles = $current_user->roles; $role = array_shift($roles); return isset($wp_roles->role_names[$role]) ? translate_user_role($wp_roles->role_names[$role] ) : false; } –  OriginalEXE Jun 21 '12 at 16:28
1  
this only solve part of my question, the next thing is to query the posts by the role. Your codes will be clumsy as I did. Which is not what I wanted. –  dev-jim Jun 21 '12 at 17:27

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.