Help Readers Stay Within their WordPress Search Results Using this Simple Concept

Everyone knows that content is king, and one of the ways that you can keep your site fresh is to maintain a blog on your website. This is a great way to engage your audience with relative information to your market, news about your company, and even tips and advice on how to use your service or product.

Once you’ve built up some content, your readers may find themselves searching your site for blog posts relevant to them. But we all know that the first result isn’t always exactly what we’re looking for, or perhaps not the only one that we want to read. So I’d like to present a concept we can use with WordPress that will help readers continue traversing through the results their search returns.

Capture the Results

The first thing we want to do is create a function we can use to retrieve the ID’s of the posts that our search returns.
[php]function ut_search_array( $search_hash ) {
// check for existence of unique transient
if ( false === ( $search_array = get_transient( ‘ut_search_’ . $search_hash ) ) ) {
global $wpdb;

$posts = $wpdb->get_results( $GLOBALS[‘wp_query’]->request );

$search_array = array();

if ( false === empty( $posts ) ) {
foreach ( $posts as $post ) {
$search_array[] = $post->ID;

// save the transient for 10 minutes
set_transient( ‘ut_search_’ . $search_hash, $search_array, MINUTE_IN_SECONDS * 10 );

return $search_array;
This function starts out checking to see if a transient unique to this search result exists. If it doesn’t, we create it using a unique string, the `$search_hash`. This function will return an array of post ID’s. Let’s say for our example that the ID’s it returns are 2, 4, 8, 9, and 11.

Store Data in a Session

Next, we’ll need the plugin WP Session Manager. This gives us a simple way to create sessions with WordPress. We’ll store the search string that was entered into the search box, the search hash – a unique string that WordPress creates when a search is performed – and the array of posts ID’s from the function that we created above.
* Set session search_string
function ut_set_search_string() {
global $wp_query;

$wp_session = WP_Session::get_instance();

// if we’re on a search page, save the search data
if ( !is_admin() && is_search() && isset( $wp_query->query_vars_hash ) ) {
$search_string = $wp_query->query[‘s’];
$search_hash = $wp_query->query_vars_hash;
$wp_session[‘search_string’] = $search_string;
$wp_session[‘search_hash’] = $search_hash;
$wp_session[‘search_array’] = ut_search_array( $search_hash );
// if we’re anywhere else, clear the search data
if ( !is_admin() && !is_search() && !is_single() && is_main_query() ) {
$wp_session[‘search_string’] =
$wp_session[‘search_hash’] =
$wp_session[‘search_array’] = null;
add_action( ‘pre_get_posts’, ‘ut_set_search_string’ );[/php]
This is added on ‘pre_get_posts’ and will let us temporarily store data unique to the current user without them having to be a user in the WordPress database. We can now get this data anywhere else on the site. But take note to where the data is destroyed if they are on certain pages.

Find our Place in the Post ID Array

Now we’ll need a function that we can use inside a post to find out where that post is within the post ID array. So for instance, if we’re on post 4, we’re in the second position in the array. That makes the next post 8, and the previous post 2.
* Get the next or previous post in the array
function ut_get_next_search_result( $next = true ) {
$wp_session = WP_Session::get_instance();

// make sure there’s a search saved in the session
if ( isset( $wp_session[‘search_array’] ) === false ) {
return false;

// set variables
$next_key = 0;
$search_array = $wp_session[‘search_array’]->toArray();
$current_key = array_search( get_the_ID(), $search_array );

// get next or previous location in the array
if ( $next === true ) {
$next_key = $current_key + 1;

if ( isset( $search_array[$next_key] ) === false ) {
$next_key = 0;
} else {
$next_key = $current_key – 1;

if ( isset( $search_array[$next_key] ) === false ) {
end( $search_array );
$next_key = key( $search_array );

// return value from that location
return $search_array[$next_key];
This function merely returns the next or previous post ID according to where you are within the search results array. It does nothing if there is no search array stored, which would be true had we visited a page outside of the search results as set by our second function.

Create the Next and Previous Post Links

Finally, we can use all of the data we’ve stored and the functions we’ve created to give the user a chance to go backwards or forwards within their search results, instead of between all of the posts on the blog.
$prev_url = $next_url = false;
// get next and prev search results or just the links
if ( ut_get_next_search_result() ) {
$wp_session = WP_Session::get_instance();
$prev_url = get_permalink( ut_get_next_search_result(
false ) );
$next_url = get_permalink( ut_get_next_search_result() );
} else {
$prev_url = get_previous_post();
$next_url = get_next_post();
<div class=”single-nav”>
<a href=”” class=”prev”>Previous</a>
<a href=”” class=”next”>Next</a>
This checks to see if a next or previous link exists within the search results. If you’ve arrived on the current post without searching, then it will simply return the chronologically next and previous post links.

There are a few checks and balances I’ve omitted for brevity’s sake, but I hope that you’ll find this technique useful. If you have any questions about using this technique, you can connect with us on LinkedIn, Facebook or Twitter, or check out our work on GitHub.