get posts based on meta value of the author

Get for all users / authors with user meta field. meta1 = true

$args = array(
    'meta_key'     => 'meta1',
    'meta_value'   => 'true',
    'meta_compare' => '=',
    'fields'       => 'all',
 );
$users = get_users( $args );

Store user id and login into an array authors

$authors = array();
foreach ( (array) $users as $user ) {
  authors[ $user->ID ] = $user->user_login;
}

Now you can pass authors in your posts query.

$posts = query_posts( array( 'author__in'=> array_keys( $authors ) ) );

Or

$posts = new WP_Query( array( 'author__in' => array( array_keys( $authors ) ) );

Final code

$args = array(
    'meta_key'     => 'meta1',
    'meta_value'   => 'true',
    'meta_compare' => '=',
    'fields'       => 'all',
 );
$users = get_users( $args );
$authors = array();
foreach ( (array) $users as $user ) {
    $authors[ $user->ID ] = $user->user_login;
}
$query = new WP_Query( array( 'author__in' => array( array_keys( $authors ) ) );

if ( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post();

//print post contents, title etc

endwhile;
endif;
wp_reset_postdata();

Leave a Comment