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(
    'author'=>$user_id,
    'post_type'=>'any',
    'post_status'=>'publish',
    'posts_per_page'=>1,
  ));
  return (count($result->posts)!=0);
}

and then in my theme:

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

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


Solution:

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: