Check if a user is logged in using is_logged_in
. If a user is logged in modify the query using pre_get_posts
filtering posts by the current author ID.
if ( is_user_logged_in() ) :
function filter_posts_by_author( $query ) {
global $current_user;
get_currentuserinfo();
$query->set( 'author', $current_user->ID );
}
add_action( 'pre_get_posts', 'filter_posts_by_author' );
// ... loop
else :
echo 'you must be logged in to view this page';
endif;