1

So far, I need to build a custom search slug e.g by default its: example.com/?s=cat I changed it to example.com/store/query using following rewrite rules inside function.php file

function feed_dir_rewrite( $wp_rewrite ) {
    $feed_rules = array( 'store/(.+)' => 'index.php?s=' . $wp_rewrite->preg_index(1));
    $wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;
}
add_filter( 'generate_rewrite_rules', 'feed_dir_rewrite' );   

Now, everything works fine, but pagination doesn't work at all, below is the pagination code:

function Theme_pagination() {

    if (!empty($options['extra_pagination']) && $options['extra_pagination'] == '1') {

        global $wp_query;

        $total_pages = $wp_query->max_num_pages;

        if ($total_pages > 1) {

            $current_page = max(1, get_query_var('paged'));

            echo '<div id="pagination">';

            echo paginate_links(array(
                'base' => get_pagenum_link(1) . '%_%',
                'format' => '/page/%#%',
                'current' => $current_page,
                'total' => $total_pages
            ));
            echo '</div>';
        }

    } else {

        if (get_next_posts_link() || get_previous_posts_link()) {
            echo '<div class="pagination col-lg-12">';

            if (get_previous_posts_link()) : ?>
                <div class="next-post margint20 button pull-left ">
                    <?php previous_posts_link( __( 'NEWER POST ', 'Themes-fm' ) ); ?>
                </div>
            <?php endif;

            if (get_next_posts_link()) : ?>
                <div class="prev-post margint20 button pull-right">
                    <?php next_posts_link( __( 'OLDER POST', 'Themes-fm' ) ); ?>
                </div>
            <?php endif;

            echo '</div>';
        }

    }

}

Here's the quick recap of the problem:

example.com/?s=cat (Let's say there are 10 posts matching to cat keyword with 5 posts per page on search results)
Results: 5 posts on the first page>Clicked on Older Posts button and next 5 posts.
example.com/store/cat
Results: First 5 posts on the first page>clicked on Older posts and page says "No Posts Founds As Per Your Search Terms"

I need to do something to pagination or the rewrite rule? I don't have any idea what to do next... any idea? Thanks in advance... :)

1 Answer 1

2

If you want to add custom rewrite rules I'd recommend you to use available functions like add_rewrite_rule().

But!

You don't really need to add custom rewrite rules to achieve what you want.

WordPress supports nice slugs for search queries out of the box. It just needs to be "activated".

First, you need to define the URL slug like this:

function wpse293111_search_base() {
  $GLOBALS['wp_rewrite']->search_base = 'store';
}

add_action( 'init', 'wpse293111_search_base' );

After that, you need to redirect requests ?s=<searchterm> to the new URLs. You can use something like this to do so:

function wpse293111_nice_search_redirect() {
  global $wp_rewrite;
  if ( !isset( $wp_rewrite ) || !is_object( $wp_rewrite ) || !$wp_rewrite->using_permalinks() ) {
    return;
  }

  $search_base = $wp_rewrite->search_base;

  if ( is_search() && !is_admin() && strpos( $_SERVER['REQUEST_URI'], "/{$search_base}/" ) === false ) {
    wp_redirect( home_url( "/{$search_base}/" . urlencode( get_query_var( 's' ) ) ) );
    exit();
  }
}

add_action( 'template_redirect', 'wpse293111_nice_search_redirect' );

The Nice Search plugin does exactly this redirect, so you can also just install this plugin if you want and only keep the first function to change the search base.

Benefits:

  • Using built-in functionality
  • No problems with pagination
  • You can still modify search queries using pre_get_posts hook if needed

Note:

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.