WordPress.org

Ready to get started?Download WordPress

Forums

paginate query_posts (2 posts)

  1. ke vinritt
    Member
    Posted 2 years ago #

    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?
    Thanks

  2. vtxyzzy
    Member
    Posted 2 years ago #

    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:

    query_posts("cat=23&posts_per_page=5&paged=$paged");

    The array arguments method looks like this:

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

Topic Closed

This topic has been closed to new replies.

About this Topic