WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?


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

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



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

      endwhile; endif;


share|improve this question
up vote 1 down vote accepted

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

For example:

  global $current_user;
  $username = $current_user->user_login;
  $user_id = $current_user->ID;

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

   // 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


share|improve this answer
Henry, it doesn't seem to be working and I don't know why. – Dz.slick Oct 22 '12 at 16:05
Which file are you updating with the code? – henrywright Oct 29 '12 at 22:37
With few changes, it works as exactly as I want. In the end, It was your(henrywright) Idea. – Dz.slick Nov 2 '12 at 18:44
Glad you were able to customise the code to work for you! – henrywright Nov 3 '12 at 11:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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