[英]Wordpress - Make it so that non-admin users can't see posts with a specific custom post status
我將對功能文件使用什么樣的鈎子才能使其生成,以便所有非管理員用戶都無法在wp-admin后端看到具有特定自定義post_status的所有帖子。 但是仍然可以通過WordPress發布循環查詢和循環嗎?
使用pre_get_posts,您應該可以入門(從管理屏幕隱藏帖子)。 您可能還需要檢查帖子類型等。
function filter_posts( $wp_query ) {
if ( is_admin() ) {
$user = wp_get_current_user();
$post_status = 'draft';
if ( ! in_array( 'administrator', $user->roles ) ) {
$wp_query->set( 'post_status', $post_status );
}
}
}
add_action( 'pre_get_posts', 'filter_posts', 10 );
要禁止用戶編輯具有特定狀態的帖子,您應該執行以下操作:
function restrict_post_editing(){
global $post;
$post_status = 'draft';
if ( get_post_status( $post ) == $post_status ) {
$user = wp_get_current_user();
if ( ! in_array( 'administrator', $user->roles ) ) {
do_action('admin_page_access_denied');
wp_die( __('You cannot modify or delete this entry.') );
exit;
}
}
}
add_action('edit_post', 'restrict_post_editing', 10, 1);
add_action('wp_trash_post', 'restrict_post_editing', 10, 1);
add_action('before_delete_post', 'restrict_post_editing', 10, 1);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.