【问题标题】:Check if user has custom post published redirection on WordPress检查用户是否在 WordPress 上有自定义发布的重定向
【发布时间】:2019-04-05 07:56:40
【问题描述】:

我正在尝试在用户访问某个页面时进行检查,以查看当前用户是否有自定义帖子类型的已发布或草稿帖子。如果他们这样做,那么我正在尝试重定向他们。我找到了一些帖子,但没有运气将它们放在一起以满足我的需求。我可以更容易理解模板重定向功能,所以我尝试走这条路。我不确定这是否是最好的方法。

我尝试过使用这些 sn-ps:

https://wordpress.stackexchange.com/questions/187973/how-to-check-that-if-current-user-id-has-posts-or-not

https://wordpress.stackexchange.com/questions/139818/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;

      }
   }        
   }

如果当前用户转到第 1 页并且是自定义帖子类型的已发布作者或草稿作者,则重定向到第 2 页。如果当前用户转到第 1 页并且不是自定义帖子类型的已发布作者,则什么也不做,正常加载第 1 页。

谢谢。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    butlerblog提供的解决方案

    
    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();
    
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-12
      • 1970-01-01
      • 1970-01-01
      • 2015-12-20
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      相关资源
      最近更新 更多