以下是对wp-admin
这应该可以做到:
/**
* Modification of the wp-admin main (post) query:
* If current user has the "client" role then
* show client\'s posts OR posts where the "client" meta value is the client id.
*
* @see http://wordpress.stackexchange.com/a/173967/26350
*/
function wpse_pre_get_posts( $q )
{
if( is_admin()
&& $q->is_main_query()
&& \'post\' === $q->get( \'post_type\' )
&& ! current_user_can( \'administrator\' )
)
{
$q->set( \'author\', get_current_user_id() );
if( current_user_can( \'client\' ) )
{
$q->set( \'author\', null );
$q->set( \'meta_key\', \'client\' );
$q->set( \'meta_value\', get_current_user_id() );
add_filter( \'posts_where\', \'wpse_posts_where\' );
}
}
return $query;
}
add_filter( \'pre_get_posts\', \'wpse_pre_get_posts\' );
其中
posts_where
过滤器定义为:
function wpse_posts_where( $where )
{
global $wpdb;
remove_filter( current_filter(), __FUNCTION__ );
return str_ireplace(
"{$wpdb->postmeta}.meta_key",
sprintf(
"{$wpdb->posts}.post_author = %d
OR {$wpdb->postmeta}.meta_key",
get_current_user_id()
),
$where
);
}
我想那里有
no 主菜单上的其他元查询
wp-admin
. 如果不是这样的话,那么您应该能够进一步细化它。