I have a custom search results page that has the url "/mysearch/location/SUBMITTED DATA/" which works perfectly fine.

However I'm now trying to get my pagination working on that page so the URL will look as follows - "/mysearch/location/SUBMITTED DATA/page/2/" but when I visit that URL all I seem to recieve is a "404 page not found".

My current rewrite is performed with the following code:

function add_search_store_query_var($vars) {
   $vars[] = 'location';
   return $vars;
add_filter( 'query_vars', 'add_search_store_query_var');

function add_search_store_rewrite_rule() {
    add_rewrite_rule('mysearch/([^/]+)/([^/]+)$', 'index.php?page_id=200&location=$matches[1]', 'top');
add_action('init', 'add_search_store_rewrite_rule');

Here is how my current pagination links are generated:

    $total_query    =   "SELECT COUNT(1) FROM (${query}) AS combined_table";
    $total          =   $wpdb->get_var( $total_query );
    $items_per_page =   1;
    $page           =   get_query_var( 'paged' ) > 1 ? get_query_var('paged') : 1;
    $offset         =   ( $page * $items_per_page ) - $items_per_page;
    $searchResults  =   $wpdb->get_results( $query . " HAVING distance < ".$distance." ORDER BY distance LIMIT ${offset}, ${items_per_page}" );

    $paginationOptions = array(
        'base'          => @add_query_arg( 'paged', '%#%' ),
        'format'        => '',
        'prev_text'     => __('&laquo;'),
        'next_text'     => __('&raquo;'),
        'total'         => ceil($total / $items_per_page),
        'current'       => $page,

     if ( $wp_rewrite->using_permalinks() )
        $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ).'page/%#%/', 'paged' );

    if ( ! empty( $wp_query->query_vars['s'] ) )
        $pagination['add_args'] = array( 's' => get_query_var( 's' ) );

If anybody has any suggestions or ideas on how I can solve this then it would be greatly appreciated.

Thanks, Codarz360

1 Answer 1


I finally figured out how to fix the issue I had.

I added the following into my rewrite_rules function:


So now my URLs are exactly as I wanted them: /mysearch/location/SUBMITTED-INFO/page/WHATEVER

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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