Which hook do I use, to redirect user based on post/category?

254 Views Asked by At

I'm new to WP development. I need to write a hook to check if the currently logged in user is viewing a post listed within a specific category, and then redirect user if they're lacking certain meta data.

I tried creating this function:

add_action('init','check_user_post_category');

however inside that function I was unable to get the post object (I have tried everything I found on the web!)

global $post; // This object is not valid at this time
global $wp; // $wp->request is empty
$_REQUEST; // This var is giving me an empty array! Is this normal??? :(

Could you kindly suggest, what hook is best to use in this case, and how to get the post object? Many thanks!

1

There are 1 best solutions below

1
On BEST ANSWER

Use 'wp' hook instead of 'init'.

add_action('wp','check_user_post_category');

Maybe this would work for you.