The WP_Query Object does not support retrieving posts by author_meta
so neither will the pre_get_posts
hook. The next best thing would be to run a quick WP_User_Query and get all users by the specific author_meta
then limit our query to only pull posts from those authors. Here’s what that would look like:
/**
* Modify Queries
*
* @param WP_Query Object $query
*/
function theme_pgp( $query ) {
// If we're on admin side, return
if( is_admin() ) {
return;
}
// Only apply to main queries
if( $query->is_main_query() ) {
// Get all users with a Company field
$users = new WP_User_Query( array(
'meta_key' => 'company',
'meta_value' => 'company1',
'fields' => 'ID',
) );
$authors = $users->get_results();
if( ! empty( $authors ) ) {
$query->set( 'author__in', $authors );
}
}
}
add_action( 'pre_get_posts', 'theme_pgp' );