Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

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?

Cheers

1 Answer

Robert Bojor
PLUS
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
}
wp_reset_postdata();

Hope that helps.