Traversing
Search Results
with Sessions + Transients
Tammy Hart
Web Designer + Developer
at UpTrending
@tammyhart
Objective:
1. Temporarily store search results
2. Navigate search results with next
and previous post links
You will need:
• WordPress site with several posts
• WP Session Manager Plugin by Eric Mann
1 2 3
654
987
121110
? =
loopconf_search_array( $search_hash )
=
array( 2, 4, 8, 9, 11 )
/**
* Return search array
*/
function loopconf_search_array( $search_hash ) {
// check for existence of unique transient
if ( false === ( $search_array = get_transient(
'loopconf_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( 'loopconf_search_' . $search_hash,
$search_array, MINUTE_IN_SECONDS * 10 );
}
return $search_array;
}
?
unique string
posts array
session
$wp_session['search_string'] = 'cats'
$wp_session['search_hash'] = 'lkmvdsoi939ks0fdm...'
$wp_session['search_array'] = array( 2, 4, 8, 9, 11 )
/**
* Set session search_string
*/
function loopconf_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'] = loopconf_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', 'loopconf_set_search_string' );
Post 4 Post 9
Post 8
WordPress ipsum dolor sit amet install brecker basie multisite coltrane wordcamp ella westi. foundation mike parker
multisite nacin wp-cli monk gershwin customizer plugin. gpl mark vaultpress boren database filter westi upgrade mike
chat strayhorn api. theme dexter baker api wp-cli action automattic jaquith parker premium oscar brecker tyner.
Donncha green plugin gershwin customizer tyner multisite buddypress helen duke database bbpress carmen blog. Mike
green carmen westi jaquith, akismet matt buddypress tyner plugin getz stitt duke. Nacin smith api tyner ozz language.
Monk boren cache, post api green reinhardt transient. Smith boren b2, api cache getz mullenweg WordPress. Davis tyner
carmen buddypress, mike chat smith api theme meetup mingus install stitt. Boren jones basie, chat otto bbpress filter
stitt.
Dexter wordcamp jazz plugin davis oscar. Akismet mike jen customizer website green. Donncha jen haiku blog website,
oscar andrew. Westi premium boren oscar, baker mike basie wordcamp upgrade language jen gpl ella dexter.
if ( $go_forward ) { return 9 }
else { return 4 }
/**
* Get the next or previous post in the array
*/
function loopconf_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];
}
<?php
$prev_url = $next_url = false;
// get next and prev search results or just the links
if ( loopconf_get_next_search_result() ) {
$wp_session = WP_Session::get_instance();
$prev_url = get_permalink( loopconf_get_next_search_result(
false ) );
$next_url = get_permalink( loopconf_get_next_search_result() );
} else {
$prev_url = get_previous_post();
$next_url = get_next_post();
}
?>
<div class="single-nav">
<a href="<?php echo esc_url( $prev_url ); ?>"
class="prev">Previous</a>
<a href="<?php echo esc_url( $next_url ); ?>"
class="next">Next</a>
</div>
Live example:
weststudio.com
Thanks!
bit.ly/traverse_loopconf
@uptrending / @tammyhart

Traversing Search Results

  • 1.
  • 2.
    Tammy Hart Web Designer+ Developer at UpTrending @tammyhart
  • 3.
    Objective: 1. Temporarily storesearch results 2. Navigate search results with next and previous post links
  • 4.
    You will need: •WordPress site with several posts • WP Session Manager Plugin by Eric Mann
  • 5.
  • 6.
  • 7.
    /** * Return searcharray */ function loopconf_search_array( $search_hash ) { // check for existence of unique transient if ( false === ( $search_array = get_transient( 'loopconf_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( 'loopconf_search_' . $search_hash, $search_array, MINUTE_IN_SECONDS * 10 ); } return $search_array; }
  • 8.
  • 9.
    $wp_session['search_string'] = 'cats' $wp_session['search_hash']= 'lkmvdsoi939ks0fdm...' $wp_session['search_array'] = array( 2, 4, 8, 9, 11 )
  • 10.
    /** * Set sessionsearch_string */ function loopconf_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'] = loopconf_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', 'loopconf_set_search_string' );
  • 11.
    Post 4 Post9 Post 8 WordPress ipsum dolor sit amet install brecker basie multisite coltrane wordcamp ella westi. foundation mike parker multisite nacin wp-cli monk gershwin customizer plugin. gpl mark vaultpress boren database filter westi upgrade mike chat strayhorn api. theme dexter baker api wp-cli action automattic jaquith parker premium oscar brecker tyner. Donncha green plugin gershwin customizer tyner multisite buddypress helen duke database bbpress carmen blog. Mike green carmen westi jaquith, akismet matt buddypress tyner plugin getz stitt duke. Nacin smith api tyner ozz language. Monk boren cache, post api green reinhardt transient. Smith boren b2, api cache getz mullenweg WordPress. Davis tyner carmen buddypress, mike chat smith api theme meetup mingus install stitt. Boren jones basie, chat otto bbpress filter stitt. Dexter wordcamp jazz plugin davis oscar. Akismet mike jen customizer website green. Donncha jen haiku blog website, oscar andrew. Westi premium boren oscar, baker mike basie wordcamp upgrade language jen gpl ella dexter.
  • 12.
    if ( $go_forward) { return 9 } else { return 4 }
  • 13.
    /** * Get thenext or previous post in the array */ function loopconf_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 ); ...
  • 14.
    ... // get nextor 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]; }
  • 15.
    <?php $prev_url = $next_url= false; // get next and prev search results or just the links if ( loopconf_get_next_search_result() ) { $wp_session = WP_Session::get_instance(); $prev_url = get_permalink( loopconf_get_next_search_result( false ) ); $next_url = get_permalink( loopconf_get_next_search_result() ); } else { $prev_url = get_previous_post(); $next_url = get_next_post(); } ?> <div class="single-nav"> <a href="<?php echo esc_url( $prev_url ); ?>" class="prev">Previous</a> <a href="<?php echo esc_url( $next_url ); ?>" class="next">Next</a> </div>
  • 16.
  • 17.