Skip to main content
changed add_filter() -> add_action()
Source Link

This can be accomplished via the parse_query filter, as follows:

add_filteradd_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.

This can be accomplished via the parse_query filter, as follows:

add_filter ('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.

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.

Source Link

This can be accomplished via the parse_query filter, as follows:

add_filter ('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.