Support » Fixing WordPress » paginate query_posts

  • I am using custom post types for an ‘events’ page. The events are ordered by their date. Iam using the query_posts function to do this.
    <?php query_posts( 'post_type=events&meta_key=date&orderby=meta_value&order=ASC&posts_per_page=5'); ?>
    However, is there a way to add pagination? I want to show 5 events per page and then paginate. Also, is there a way to delete past date posts?

Viewing 1 replies (of 1 total)
  • Pagination requires three things:

    1. Allowing the viewer to select a page number.
    2. Detecting the requested page number.
    3. Using the page number in the query.

    The first part can be done using the built-in WP function paginate_links() or a plugin such as WP-PageNavi.

    Part two is done by retrieving the query variable ‘page’:

    <?php $paged = (get_query_var('page')) ? get_query_var('page') : 1; ?>

    Part three is done by adding the ‘paged’ variable to the query. There are two formats commonly used to pass the argument to query_posts(): a query string or an array. The string method is similar to this:


    The array arguments method looks like this:

    $args = array(
       'cat' => 23,
       'posts_per_page' => 5,
       'paged' => $paged

Viewing 1 replies (of 1 total)
  • The topic ‘paginate query_posts’ is closed to new replies.