I’ve created a function that uses the built-in function paginate_links
.
But the pagination isn’t working correctly because the URL is being written wrong.
The URL I want looks like this domain.com/properties/page/2/?foo=bar
The URL that is being output is domain.com/properties/?foo=bar/page/2/
Here’s my code
function paginate($max_num_pages) {
global $wp_query, $wp_rewrite;
$wp_query->query_vars['page'] > 1 ? $current = $wp_query->query_vars['page'] : $current = 1;
$pagination = array(
'base' => @add_query_arg('page','%#%'),
'format' => '',
'total' => $max_num_pages,
'current' => $current,
'show_all' => true,
'end_size' => 1,
'mid_size' => 2,
'prev_next' => True,
'prev_text' => __('« Previous'),
'next_text' => __('Next »'),
'type' => 'plain',
'add_args' => false,
'add_fragment' => ''
);
if( $wp_rewrite->using_permalinks() )
$pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'page' );
if( !empty($wp_query->query_vars['s']) )
$pagination['add_args'] = array( 's' => get_query_var( 's' ) );
echo paginate_links( $pagination );
}
Here’s what ended up working for me.
I had to change the
$wp_query->query_vars
topaged
and I removed the line that checked if I was using perma linksif ( $wp_rewrite->using_permalinks() ) $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' );
According to the documentation for
paginate_links
the defaultformat
looks similar to what you’re after, but you’re overriding that value by setting'format' => ''
.Try removing that option or set it to what you’re after:
'format' => '?foo=bar%#%'