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

I'm trying to display a custom loop with a dynamic category filter.

As a setup I have categories of all usernames which are created once a user creates an account.

So I am trying to echo the user's username as a category filter. It works when I echo is elsewhere on the page, but it doesn't work when I try to embed it like so:

<?php query_posts('category_name=<?php global $current_user; if ( isset($current_user) ) {echo $current_user->user_login;}?> &posts_per_page=6'); ?>

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>  
<?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?>

<?php endwhile; else: ?>

NO Posts Present 

<?php endif; ?>

Any help would be greatly appreciated, thanks.

share|improve this question

You are opening a php tag inside an open php tag so you are actually calling this whole string: <?php global $current_user; if ( isset($current_user) ) {echo $current_user->user_login;}?> as a category and you echo it out (wrongly) which is not passing ia as a perameter to query_posts move it outside the query_posts call:

global $current_user;
query_posts(array('category_name' => $current_user->user_login,'posts_per_page'=>6));

I Would recommend catching up on basic PHP before moving on, and once you got that part down then you should avoid using query_posts altogether and use either get_posts() or wp_query read up on it

share|improve this answer

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.