On my WordPress site, users are able to create custom post types (products). I’m looking to disallow access to a page like example.com/sample by redirecting users to another page. I figured how to show a notice on example.com/sample if they didn’t create at least one post, but I couldn’t figure how to redirect them from example.com/sample to another page. Here’s what I have so far:

function yoursite_user_has_posts($user_id) {
  $result = new WP_Query(array(
  return (count($result->posts)!=0);

and then in my theme:

$user = wp_get_current_user();
if ($user->ID)
  if (yoursite_user_has_posts($user->ID))
    echo 'Thank you for writing!';
    echo 'You need to write a post!';

Read more here: Redirect to another page if user didn’t create 1 post


If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Wordpress related questions and answers: