这个WP_Query Object 不支持通过检索帖子author_meta
因此pre_get_posts
hook. 下一个最好的办法是快速WP_User_Query 并通过特定的author_meta
然后将我们的查询限制为仅从这些作者那里获取帖子。下面是它的样子:
/**
* 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\' );