[wp-hackers] WP_Query filter posts by role

Jeremy Clarke jer at simianuprising.com
Tue Sep 4 17:21:36 UTC 2012

On Mon, Aug 27, 2012 at 5:51 AM, Filippo Pisano <filippo.pisano at gmail.com>wrote:

> is there a way to filter all posts written by users with specific roles
> (editors, authors etc)?
There is no WP_Query parameter that will do what you want, so your only
option is probably to use the posts_from and posts_where filters to add in
custom SQL.

Users' roles are stored in the wp_usermeta table of the database as a
serialzed array of all their roles and/or individual capabilities. This
means you will almost definitely NOT want to access them directly. Instead
I propose that what you'd want to do is generate an array with all users in
a given role, then in the posts_where filter add a clause that fetches
posts from any of the authors from that array.

Not a simple solution but your problem is fairly unique. User roles are
intended for use in controlling access permissions rather than for marking
content sections of your site (though your issue points out how such a
'content groups' need often comes up).

Jeremy Clarke
Code and Design • globalvoicesonline.org

More information about the wp-hackers mailing list