忽略我对的评论meta_query
. 它不仅不适用于$query->set()
, 你无法控制关键的“A”OR B“查询的要求。
相反,我相信您所需要的是通过两者的结合pre_get_posts
行动挂钩和posts_where
过滤器挂钩如下。
add_action(\'pre_get_posts\', \'my_show_appropriate_posts\');
function my_show_appropriate_posts($query){
if(strpos($_SERVER[ \'REQUEST_URI\' ], \'/wp-admin/edit.php\') !== false) :
if(!current_user_can(\'manage_options\')) :
/** Unset so that we can control exactly where this part of the query is included */
$query->set(\'author\', null);
/** Ensure that the relevant tables required for a meta query are included */
$query->set(\'meta_query\', array(
array(
\'key\' => \'add_editing_permission_for\',
\'value\' => \'dummy\', // This cannot be empty because of a bug in WordPress
\'compare\' => \'=\'
)
));
/** Call the filter to amend the \'$where\' portion of the query */
add_filter(\'posts_where\', \'my_custom_posts_where\');
endif;
endif;
}
function my_custom_posts_where( $where = \'\' ){
global $wpdb;
/** Add our required conditions to the \'$where\' portion of the query */
$where.= sprintf(
\' AND ( %1$s.post_author = %2$s OR ( %3$s.meta_key = "add_editing_permission_for" AND %3$s.meta_value = %2$s ) )\',
$wpdb->posts,
get_current_user_id(),
$wpdb->postmeta
);
/** Remove the filter to call this function, as we don\'t want it any more */
remove_filter(\'posts_where\', \'my_custom_posts_where\');
return $where;
}
Recommended reading