0

I am trying to run a check when a user visits a certain page to see if the current user has a published or draft posts of a custom post type. If they do, then I'm trying to redirect them. I've found some posts, but no luck in putting it all together to fit my needs. I can understand the template redirect function a bit easier, so I've tried to go that route. I'm not sure if that's the best way to do it.

I've tried to work with these snippets:

Redirect based on user post count

How to check that if current user (ID) has posts or not

Check if current user has post in post type and is author role

add_action( 'template_redirect', 'redirect_to_specific_page_resume' );

function redirect_to_specific_page_resume() {
global $post;

$current_user = $post->post_author;

if(!empty($current_user)){
$user_post_count = (int) count_user_posts( $current_user );

   if ( is_page('479') && $user_post_count == 1 ) {

        wp_redirect( "/myaccount/manage-resumes", 301 ); 

        exit;

  }

If current user goes to PAGE ONE and is a published or draft author of a custom post type, then redirect to PAGE TWO. If current user goes to PAGE ONE and is NOT a published author of a custom post type, do nothing, load PAGE ONE as normal.

Thank you.

1

If I understand your question and your code correctly, I think your issue is the following line:

$current_user = $post->post_author;

You're evaluating the current user AS the post author. So your logic evaluation then checks "if the page is 479 and the author ID of post ID 479 has only published 1 post, then redirect". I don't think that's what you're intending.

Also, your use of count_user_posts() doesn't check for a post type - but you're asking about the user being an author of a custom post type. What is the custom post type? Wouldn't that be what you check with count_user_posts()? If a CPT slug is not passed in that function, it will only check for "posts". My example below makes the assumption that you want to check the post type of the is_page() value you're checking (479).

And (lastly?), you're checking if the user has a draft or published post type equal to (==) 1? What if it's 2? I suspect the logic here should be "greater than" (>) instead.

So there's a lot of unanswered questions and some definite gray areas in your question, but here's a stab at something based on what I can guess:

add_action( 'template_redirect', 'redirect_to_specific_page_resume' );

function redirect_to_specific_page_resume() {

    /*
     * Only bother to check if the user is logged in 
     * AND we're on page ID 479. Otherwise, there's no
     * reason to run the remaining logic.
     */
    if ( is_user_logged_in() && is_page( 479 ) ) {

        // Get the current logged in user's ID
        $current_user_id = get_current_user_id();

        // Count the user's posts for 'resume' CPT
        $user_post_count = (int) count_user_posts( $current_user_id, 'resume' );

        // If the user has a 'resume' CPT published
        if ( $user_post_count > 1 ) {

            // Get the URL to redirect the user to.
            $url = get_permalink( 480 );

            // Redirect the user.
            wp_redirect( $url ); 
            exit();

        }
    }
}
  • Hi there, thank you so much for responding. The code is probably a big mess, I tried to piece it together with several snippets. :-) This might explain it better: If current user goes to PAGE 479 and has a published or draft author of a custom post type - resume, then redirect to PAGE 480. If current user goes to PAGE 479 and is NOT a published or draft author of a custom post type - resume, do nothing, load PAGE 479. I'm not trying to check the post type/author for page 479. If a user tries to access page 479 and has a draft or published custom post type resume - then redirect to 480. – joy Apr 4 at 22:39
  • That clears some things up. The CPT is "resume" correct? I've cleaned up the code in the answer to reflect that, and also to redirect to the page with 480 as the post ID. I've commented each step so you can follow the logic. One last thing to clear up - you're using is_page() to check whether we're on page ID 479. But that only works if that page is a "page" post type. If it's a "post" then that won't work (and will always return false). – butlerblog Apr 4 at 23:01
  • It works!! Thank you so much, I've been trying to get this figured out for days. I appreciate your help! – joy Apr 4 at 23:53
  • No problem - glad it worked out for you. – butlerblog Apr 5 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.