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.

1 Answer 1


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

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.