【WordPress】自分の投稿のみ記事一覧に表示させる

電脳備忘録

権限を制限したユーザーがそれなりにいて、彼ら他者の記事を閲覧できない様にした。下記をfunction.phpに追記。

function exclude_other_posts( $wp_query ) {
if (!current_user_can('administrator')) {
if ( isset( $_REQUEST['post_type'] ) && post_type_exists( $_REQUEST['post_type'] ) ) {
$post_type = get_post_type_object( $_REQUEST['post_type'] );
$cap_type = $post_type->cap->edit_other_posts;
} else {
$cap_type = 'edit_others_posts';
}
if ( is_admin() && $wp_query->is_main_query() && ! $wp_query->get( 'author' ) && ! current_user_can( $cap_type ) ) {
$user = wp_get_current_user();
$wp_query->set( 'author', $user->ID );
}
}
}
add_action( 'pre_get_posts', 'exclude_other_posts' );

サイト管理者の権限では登録された全ての記事が一覧に表示されるが、制限ユーザーにおいては自分が登録した記事位以外は一覧表示されなくなった。とりあえずはこれでよし。

広告

ブログの維持費に充てるでございます・・・。