WordPress.org

Ready to get started?Download WordPress

Forums

Trouble adding posts_per_page limit to a query (3 posts)

  1. brian317
    Member
    Posted 2 years ago #

    I can't get the posts_per_page to blend into this query... can't find any writing on this script which is just using "query" instead of "query_posts", so confused, please help!

    $tempWPQuery = $wp_query;
    $wp_query = null;
    
    $wp_query = new WP_Query();
    
    // Issue a query that returns 4 posts in a particular category
    $wp_query->query('category_name=featured-item');
    
    while ($wp_query->have_posts()):
    //display stuff instructions..
  2. Jacob Chappell
    Member
    Posted 2 years ago #

    To add additional parameters to the query, simply separate them by the & symbol. For example:

    $wp_query->query('category_name=featured-item&posts_per_page=5');

    The above example limits the query to return only 5 results. As is, this will only display 5 posts on the first page. If you want additional pages to work, you need to pass a long the paged paramter which essentially says which page to display posts from:

    $paged = ( $wp_query->get('paged') ) ? $wp_query->get('paged') : 1;
    $wp_query->query('category_name=featured-item&posts_per_page=5&paged=' . $paged);
  3. brian317
    Member
    Posted 2 years ago #

    ohhhh thank you so much Jacob, I had almost tried that, mine had two sets of single-quotes separated by a comma and it wasn't working that way.

    i hate to be a given-an-inch-takin-a-mile guy but any chance this other puzzle of mine makes sense to you? http://wordpress.org/support/topic/the_post_thumbnail-in-get_children-query?replies=1

    thanks either way!

Topic Closed

This topic has been closed to new replies.

About this Topic