Logged in user ID as post ID

Is there a way to automatically insert the current user ID as the post category name or maybe have WordPress display only the post with a category name that matches the current logged user ID?

<?php

    $query = new WP_Query('category_name=current-user-id');

    if($query->have_posts()) : while($query->have_posts()) : $query->the_post();

    ?> 

     <?php

       the_content( __('Read the rest of this page »', 'template'));

      endwhile; endif;

     wp_reset_postdata();

?>

Related posts

Leave a Reply

1 comment

  1. You can get information on the currently logged in user using the get_currentuserinfo() function.

    For example:

    <?php
      global $current_user;
      get_currentuserinfo();
      $username = $current_user->user_login;
      $user_id = $current_user->ID;
    ?>
    

    You can then use $username or $user_id in your custom loop.

    <?php 
       // assign the variable as current category
       $category = $user_id;
    
      // concatenate the query
      $args = 'cat=' . $category;
    
      // run the query
      query_posts( $args );
    
      if ( have_posts() ) : while ( have_posts() ) : the_post(); 
    
     // do something here
    
     endwhile;
    
     endif;
     wp_reset_query(); 
    ?>