WordPress Snippet

ワードプレスをカスタマイズしよう

管理者以外は、ログインユーザーの投稿を表示する

iindivisual-post

 
add_action( 'pre_get_posts', 'query_set_only_author' );

function query_set_only_author( $wp_query ) {
	global $current_user;
	if ( !current_user_can( 'administrator' ) && is_admin() ) {
		$wp_query->set( 'author', $current_user->ID );
		$screen= get_current_screen();
		add_filter( 'views_' . $screen->id, 'indivisual_post_counts' );
	}
}

function indivisual_post_counts( $views ) {
	global $current_user, $wp_query;
	$screen= get_current_screen();
	$views= array();
	$class= '';
	$types	= array( 'all', 'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash', );
	$html	= '<a href="%1$s" %3$s>%4$s<span class="count">(%2$d)</span></a>';
	foreach ( $types as $type ) {

		$sticky			= get_option( 'sticky_posts' );
		$query			= array(
			'author'		=> $current_user->ID,
			'post_type'		=> 'post',
			'post_status'	=> $type,
			'post__not_in'	=> $sticky,
		);
		$result			= new WP_Query( $query );
		if( array_search( $wp_query->query_vars[ 'post_status' ], $types ) ) {
			
			$class			= $wp_query->query_vars[ 'post_status' ];
		} 
		
		if ( !empty( $result->found_posts) && 'edit'== $screen->base ) {
			$views[ $type ]	= sprintf( $html, 
								admin_url( 'edit.php?post_status=' . $type . '&post_type=post' ), 
								$result->found_posts, 
								current_class_helper( $type, $class, false ), 
								ucfirst( $type ) 
							);
		}
		wp_reset_postdata();
	}
	return $views;
}

function current_class_helper( $helper, $current, $echo ) {
	if ( (string) $helper=== (string) $current )
		$result= ' class="current"';
	else
		$result= '';

	if ( $echo )
		echo $result;

	return $result;
}

備考:メディアライブラリをリスト形式で開いた時に、フィルターのセレクトボックスは、すべての選択肢が表示されてしまう。

https://ja.forums.wordpress.org/topic/152894?replies=3

[emulsion_relate_posts]