Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


Jeremy Frimond
Jeremy Frimond
14,470 Points

how to control order of posts displayed in wp-loop

I would like my posts to display Where old posts display at the top and new posts would be displayed underneath.

Simultaneously I would only like to display a maximum of 5 posts at a time.

How does one go about doing this? any videos on site that teach this?


1 Answer

Robert Bojor
Robert Bojor
Courses Plus Student 29,439 Points

Hi Jeremy,

I believe you need to use a custom WP_Query in order to achieve the desired result. You can read more about it in the WP Codex ( http://codex.wordpress.org/Class_Reference/WP_Query ). The conditions and order arguments are found somewhere near the bottom of the page under the heading "Order & Orderby Parameters".

As for the number of the posts showing, you will need to also add some pagination arguments to the WP_Query by using the options found under "Pagination Parameters" heading, on the same page linked above.

A broad example of what you need is similar to the following code

$query = new WP_Query( array ( 'orderby' => 'date', 'order' => 'ASC', 'posts_per_page' => 5) );
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        // Your listing logic and HTML
} else {
    // No posts found logic

Hope that helps.