Grow your CSS skills. Land your dream job.

WordPress – query_posts issue

  • # July 22, 2012 at 3:15 pm

    I’m trying to display a list of recent posts from a specific category. I’ve been scouring all over the interweb looking for solutions, and everything I try only works half way.

    No matter what I do, the resulting list displays 6 posts, regardless of what I set it to. Both of the following seem to work, yet they both return 6 posts. At first I thought I must be missing something, but after trying countless different options, and noticing that it’s always 6 posts, I’m thinking there’s something else at work. Any ideas?


      < ?php query_posts('category_name=portfolio &posts_per_page=4'); ?>
      < ?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    • ">< ?php the_title(); ?>

    • < ?php endwhile; endif; ?>


    < ?php
    $recentposts = get_posts('numberposts=3&category_name=portfolio');
    foreach ($recentposts as $post) :
    setup_postdata($post); ?>
  • ">< ?php the_title(); ?>

  • < ?php endforeach; ?>
    # July 22, 2012 at 3:33 pm

    In the first query_posts there should be no spaces after ‘portfolio’.

    Also, you should be clearing your query_posts with:

    < ?php wp_reset_query(); ?>
    # July 22, 2012 at 3:54 pm

    I noticed the blank space as well but figured that wasn’t the problem (at this point my priority is to just get it working).

    I’m also very rusty right now, but I don’t remember having such issues with simple things like this.

    Is this more proper?


      < ?php query_posts('category_name=news&posts_per_page=3'); ?>
      < ?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    • ">< ?php the_title(); ?>

    • < ?php endwhile; endif; ?>< ?php wp_reset_query(); ?>

    Still doesn’t work. There’s 4 posts in that category and all 4 are listed rather than 3

    # July 22, 2012 at 6:08 pm

    Try showposts=3 rather than posts_per_page=3

    # July 22, 2012 at 7:18 pm

    PERFECT! That seems to do the trick.

    Thanks for the help to both of you!

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".