Say I have a user meta field that is called "meta1". I want to get posts (via get_posts or the like) who's author meta1 = true. Any idea how I should approach this?
3 Answers
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();
This can be accomplished via the parse_query filter, as follows:
add_action ('parse_query', array ($this, 'wpse_private_author_query')) ;
function
wpse_private_author_query ($query)
{
// get our private author query query_var
$private_author_query = $query->get ('_private_author_query') ;
if (empty ($private_author_query)) {
// our private author query query_var is not set, so bail without modifying $query
return ;
}
$args = array (
'fields' => 'ID',
) ;
$args = wp_parse_args ($args, $private_author_query) ;
// get the ID's of the users that match our private author query
$users = get_users ($args) ;
// unset our private author query query_var
unset ($query->query_vars['_private_author_query']) ;
// add the author IDs to the query
$query->set ('author__in', $users) ;
return ;
}
To see this in action, we can do:
$args = array (
'post_type' => 'any',
'post_status' => 'public',
'_private_author_query' => array (
'meta_key' => 'meta1',
'meta_value' => true,
'meta_compare' => '=',
),
) ;
$posts = new WP_Query ($args) ;
Note: the above is more general than your question. You asked just for user_meta, but the above will allow searching by any of the characteristics supported by WP_User_Query::parse_query(). For example, we can also do:
$args = array (
'post_type' => 'any',
'post_status' => 'public',
'_private_author_query' => array (
'role' => 'editor',
),
) ;
$posts = new WP_Query ($args) ;
etc.
-
note: my method is essentially the same as the answer from @AbdulRehman. it's just done via a
parse_request
filter. Commented Mar 21, 2017 at 14:57 -
1Thank you @Paul. this approach looks more professional. i have not used
parse_query
action hook for such queries before. will definitely use it from now. thanks again .. and @Gal my answer may also works fine but i recommend this answer.– Anwer ARCommented Mar 21, 2017 at 17:09 -
@AbdulRehman I'm not sure about "prorfessional", but it sure makes the main code cleaner to read (which I guess is one characteristic of "professional" :-) Commented Mar 21, 2017 at 17:49
You can define it in the loop's arguments and i believe it will work with other WP functions as well
$args = array(
'author' => 1 // Author's ID
);