How Custom query page template

Custom query page template

This one might sounds basic at first, but custom queries are something you often use when building WordPress based websites, so knowing how to create custom queries page templates is a must for all developers working with WordPress.

This especially came in handy for pages like my Portfolio. One special thing to take note of is the fact that I don’t simply list all of the entries on one page, but instead break it up into sets by paginating the results of the query. This is simple to do, and I’m going to show you how.
Create a Page Template

First off, you’ll need to start with a custom template, and the easiest way to do that is duplicate your normal page template, page.php. In this file, add the following code above the call for the header:

< ?php /* Template Name: ANY NAME YOU WANT */ ?>

Now you can add any customizations to it that you want and then assign that template to a page by selecting it from the “template” dropdown in the Attributes panel as is shown in the above image.
Create a Custom Loop

So now that we have our custom template, let’s put in the custom query that calls posts from the category with the ID of 3. This would typically look like this:

< ?php $temp = $wp_query; $wp_query= null; $wp_query = new WP_Query(); $wp_query->query(‘cat=3’);
?>

< ?php while ($wp_query--->have_posts()): $wp_query->the_post(); ?>

… HTML and other post stuff here…

< ?php endwhile; ?>

< ?php $wp_query = null; $wp_query = $temp; ?&gtNow, we will add in a couple more variables to the query to tell it to display just 4 posts per page. Now you’re query looks like this:query(‘cat=3&showposts=4′.’&paged=’.$paged);
?>

Don’t forget Pagination

Finally, you need to include the pagination links somewhere after the < ?php endwhile;? >. If you are using the default navigation, that will look like this:

If you’re like me, and you prefer to use the awesome plugin by Lester Chan called WP-PageNavi, then that looks like this:

< ?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>

Link: http://www.tammyhartdesigns.com/tutorials/wordpress-create-a-page-template-that-paginates-posts-from-a-certain-category/