最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

redirect - Check if user has custom post published redirection on WordPress

programmeradmin1浏览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.

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.

Share Improve this question asked Apr 4, 2019 at 20:52 joyjoy 51 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 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();

        }
    }
}
发布评论

评论列表(0)

  1. 暂无评论