1

I'm at a loss..

I wanna check the current users post count. If the post count is 1, the user should be redirected. I have tested the following code:

add_action( 'template_redirect', 'redirect_to_specific_page' );

function redirect_to_specific_page() {
$current_user = wp_get_current_user();
    if (empty($the_user_id)) {
        $the_user_id = $current_user->ID;
    }
$user_post_count = count_user_posts( $the_user_id );

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

        wp_redirect( 'enhytte', 301 ); 

    exit;

    }

}

However, the above code will redirect no matter the amount of posts.

What am I doing wrong?

EDIT: After pointing out my syntax error was a missing "=", it is still not working.

EDIT 2: The code is in theory working, however, once a post is added, the user_post_count is not updated, and stays at the previous value.

EDIT3: count_user_posts only count published posts, how do I check for 'pending' and 'draft'?

2 Answers 2

1
    add_action( 'template_redirect', 'redirect_to_specific_page' );

    function redirect_to_specific_page() {
    global $post;

    $current_user = $post->post_author;

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

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

            wp_redirect( 'enhytte', 301 ); 

            exit;

      }
   }        
   }

authorized user: $current_user = wp_get_current_user();

12
  • Haha, of course. Completely forgot that. However, it is still not working for some reason. If I change it to >= 1, the redirect is done all the time regardless of posts. == never redirects.. I am checking for a custom post type, should this type be specified somewhere maybe?
    – Beyond55
    Commented Feb 20, 2017 at 11:17
  • is_page('butiktest') works correctly? Check out: var_dump(is_page('butiktest'), $user_post_count);
    – TrubinE
    Commented Feb 20, 2017 at 11:20
  • Not sure if I did it correctly, but on the frontpage, the values are: bool(false) string(1) "2", this is on a user with 1 post. Once I click the button, where the redirection should happen, the values become: bool(true) string(1) "2". I was expecting an integer and not a string?
    – Beyond55
    Commented Feb 20, 2017 at 11:35
  • you $user_post_count is 2 and that you check == 1.
    – TrubinE
    Commented Feb 20, 2017 at 11:48
  • If I change the value to 2, it comes with a PHP error in some of the theme files. Should it not be an integer and not a string?
    – Beyond55
    Commented Feb 20, 2017 at 11:52
1

You simply have a typo, should be:

$user_post_count == 1
3
  • What is your answer differs from mine?
    – TrubinE
    Commented Feb 20, 2017 at 10:40
  • Submitted it as I checked the solution when the answers were still 0, @Beyond55, please mark TrubinE's answer as correct.
    – Svartbaard
    Commented Feb 20, 2017 at 10:42
  • Plus for honesty
    – TrubinE
    Commented Feb 20, 2017 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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