Forbid certain users to access a specific page

This code will boot out a user if they haven’t authored a post. For the purposes of this example, posts with a status of publish are considered “authored”.

I’ve added some comments in the code to walk you through what’s going on – please reach out to me if you have any questions.

<?php

// Ensure non-published users can't see what's going on.
add_action( 'init', function() {
    // Don't run in the admin area.
    if ( is_admin() ) {
        return;
    }

    // Only display this on "single" pages.
    if ( ! is_single() ) {
        return;
    }

    $user_id = get_current_user_id();

    // The user isn't a user, bail.
    if ( ! $user_id ) {
        return;
    }

    $query = <<<SQL
SELECT
    COUNT(*) published
FROM
    {$GLOBALS['wpdb']->posts}
WHERE
    post_author = %d
AND
    post_type = %s
AND
    post_status="publish"
SQL;

    $result = $GLOBALS['wpdb']->get_results(
        $GLOBALS['wpdb']->prepare(
            $query,
            $user_id,
            'post' // Replace this with your Custom Post Type, if you're not using Post.
        )
    );

    // The current user has author posts, bail.
    if ( ! empty( $result ) ) {
        return;
    }

    // Handle code to kick people out.
    wp_safe_redirect( '/error-page/' );
    exit;
} );