0

This question already has an answer here:

I'm trying to display numbered pagination for search results of a custom post type. Basically a search form allows users to search a custom post type by 2 different taxonomies and then the results are displayed on a page template. I want to display 10 posts per page.

I've managed to get the pagination to work, but only if I hard code the tag and I can't seem to figure out how to get it to automatically add in the links depending on how many pages of results there are.

Pastebin of page template code - http://pastebin.com/XpKKCdw9

marked as duplicate by fuxia Apr 8 '16 at 5:14

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • before we talk about your pagination, what happen with your custom page if both parameter location and service are not set yet? – Jevuska Apr 8 '16 at 0:28
  • Please do not use external service to host code. If the paste is removed, all context of your question is lost. It also p!$$ people off as it is really irritating having to open multiple browser windoows just to read one post. I personally just skip such questions and do not even look at them ;-) – Pieter Goosen Apr 8 '16 at 6:40
1

You need to set your paged arg in your $args array:

$listing_args = array(
    'post_type'         => 'business',
    'posts_per_page'    => 10,
    'meta_key'          => 'listing_num',
    'orderby'           => 'meta_value_num',
    'order'             => 'DESC',
    'offset'            => ($paged -1) * 10,
    'paged'             => $paged, // <- tell the query what page we are on.
    'tax_query'         => ......etc
);

And then use the pageinate_links function, https://codex.wordpress.org/Function_Reference/paginate_links to populate the links to the next/prev pages.

Example from the codex on how to use it with a custom query:

$big = 999999999; // need an unlikely integer
echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $the_query->max_num_pages
) );

Not the answer you're looking for? Browse other questions tagged or ask your own question.