SlideShare a Scribd company logo
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

More Related Content

What's hot

Legacy applications - 4Developes konferencja, Piotr Pasich
Legacy applications  - 4Developes konferencja, Piotr PasichLegacy applications  - 4Developes konferencja, Piotr Pasich
Legacy applications - 4Developes konferencja, Piotr Pasich
Piotr Pasich
 
News of the Symfony2 World
News of the Symfony2 WorldNews of the Symfony2 World
News of the Symfony2 World
Fabien Potencier
 

What's hot (20)

Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
 
Mongo à la Resque
Mongo à la ResqueMongo à la Resque
Mongo à la Resque
 
全裸でワンライナー(仮)
全裸でワンライナー(仮)全裸でワンライナー(仮)
全裸でワンライナー(仮)
 
Legacy applications - 4Developes konferencja, Piotr Pasich
Legacy applications  - 4Developes konferencja, Piotr PasichLegacy applications  - 4Developes konferencja, Piotr Pasich
Legacy applications - 4Developes konferencja, Piotr Pasich
 
Perl 6 by example
Perl 6 by examplePerl 6 by example
Perl 6 by example
 
News of the Symfony2 World
News of the Symfony2 WorldNews of the Symfony2 World
News of the Symfony2 World
 
Symfony 2.0 on PHP 5.3
Symfony 2.0 on PHP 5.3Symfony 2.0 on PHP 5.3
Symfony 2.0 on PHP 5.3
 
Webrtc mojo
Webrtc mojoWebrtc mojo
Webrtc mojo
 
Advanced Querying with CakePHP 3
Advanced Querying with CakePHP 3Advanced Querying with CakePHP 3
Advanced Querying with CakePHP 3
 
16.mysql stored procedures in laravel
16.mysql stored procedures in laravel16.mysql stored procedures in laravel
16.mysql stored procedures in laravel
 
Creating a compiler in Perl 6
Creating a compiler in Perl 6Creating a compiler in Perl 6
Creating a compiler in Perl 6
 
Perl6 in-production
Perl6 in-productionPerl6 in-production
Perl6 in-production
 
NOTEPAD MAKING IN PAYTHON 2ND PART BY ROHIT MALAV
NOTEPAD MAKING IN PAYTHON 2ND PART BY ROHIT MALAVNOTEPAD MAKING IN PAYTHON 2ND PART BY ROHIT MALAV
NOTEPAD MAKING IN PAYTHON 2ND PART BY ROHIT MALAV
 
TDC2015 Porto Alegre - Automate everything with Phing !
TDC2015 Porto Alegre - Automate everything with Phing !TDC2015 Porto Alegre - Automate everything with Phing !
TDC2015 Porto Alegre - Automate everything with Phing !
 
New in cakephp3
New in cakephp3New in cakephp3
New in cakephp3
 
Perl5i
Perl5iPerl5i
Perl5i
 
Pop3ck sh
Pop3ck shPop3ck sh
Pop3ck sh
 
Phing - PHP Conference 2015
Phing -  PHP Conference 2015Phing -  PHP Conference 2015
Phing - PHP Conference 2015
 
Perl Bag of Tricks - Baltimore Perl mongers
Perl Bag of Tricks  -  Baltimore Perl mongersPerl Bag of Tricks  -  Baltimore Perl mongers
Perl Bag of Tricks - Baltimore Perl mongers
 
I, For One, Welcome Our New Perl6 Overlords
I, For One, Welcome Our New Perl6 OverlordsI, For One, Welcome Our New Perl6 Overlords
I, For One, Welcome Our New Perl6 Overlords
 

Viewers also liked

Laying the proper foundation for plugin and theme development
Laying the proper foundation for plugin and theme developmentLaying the proper foundation for plugin and theme development
Laying the proper foundation for plugin and theme development
Tammy Hart
 

Viewers also liked (13)

Responsify!
Responsify!Responsify!
Responsify!
 
Word Press And Multimedia
Word Press And MultimediaWord Press And Multimedia
Word Press And Multimedia
 
WordPress Transients
WordPress TransientsWordPress Transients
WordPress Transients
 
Laying the proper foundation for plugin and theme development
Laying the proper foundation for plugin and theme developmentLaying the proper foundation for plugin and theme development
Laying the proper foundation for plugin and theme development
 
Dont Forget the Milk
Dont Forget the MilkDont Forget the Milk
Dont Forget the Milk
 
Responsify! 5 Things You Should Know About Responsive Web Design
Responsify! 5 Things You Should Know About Responsive Web DesignResponsify! 5 Things You Should Know About Responsive Web Design
Responsify! 5 Things You Should Know About Responsive Web Design
 
Freelancing with WordPress
Freelancing with WordPressFreelancing with WordPress
Freelancing with WordPress
 
Designing for WordPress
Designing for WordPressDesigning for WordPress
Designing for WordPress
 
Professioanl Blogging
Professioanl BloggingProfessioanl Blogging
Professioanl Blogging
 
Word Press & Working With Clients
Word Press & Working With ClientsWord Press & Working With Clients
Word Press & Working With Clients
 
Food Blog Design
Food Blog DesignFood Blog Design
Food Blog Design
 
Custom WordPress theme development
Custom WordPress theme developmentCustom WordPress theme development
Custom WordPress theme development
 
In Browser Design with WordPress
In Browser Design with WordPressIn Browser Design with WordPress
In Browser Design with WordPress
 

Similar to Traversing Search Results

The Query the Whole Query and Nothing but the Query
The Query the Whole Query and Nothing but the QueryThe Query the Whole Query and Nothing but the Query
The Query the Whole Query and Nothing but the Query
Chris Olbekson
 
You don’t know query - WordCamp UK Edinburgh 2012
You don’t know query - WordCamp UK Edinburgh 2012You don’t know query - WordCamp UK Edinburgh 2012
You don’t know query - WordCamp UK Edinburgh 2012
l3rady
 

Similar to Traversing Search Results (20)

Memory Management in WordPress
Memory Management in WordPressMemory Management in WordPress
Memory Management in WordPress
 
You Don't Know Query - WordCamp Portland 2011
You Don't Know Query - WordCamp Portland 2011You Don't Know Query - WordCamp Portland 2011
You Don't Know Query - WordCamp Portland 2011
 
[WLDN] Supercharging word press development in 2018
[WLDN] Supercharging word press development in 2018[WLDN] Supercharging word press development in 2018
[WLDN] Supercharging word press development in 2018
 
The Query the Whole Query and Nothing but the Query
The Query the Whole Query and Nothing but the QueryThe Query the Whole Query and Nothing but the Query
The Query the Whole Query and Nothing but the Query
 
Wp query
Wp queryWp query
Wp query
 
WordPress London 16 May 2012 - You don’t know query
WordPress London 16 May 2012 - You don’t know queryWordPress London 16 May 2012 - You don’t know query
WordPress London 16 May 2012 - You don’t know query
 
You don’t know query - WordCamp UK Edinburgh 2012
You don’t know query - WordCamp UK Edinburgh 2012You don’t know query - WordCamp UK Edinburgh 2012
You don’t know query - WordCamp UK Edinburgh 2012
 
Forget about loops
Forget about loopsForget about loops
Forget about loops
 
Getting Creative with WordPress Queries
Getting Creative with WordPress QueriesGetting Creative with WordPress Queries
Getting Creative with WordPress Queries
 
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい
 
You Don't Know Query (WordCamp Netherlands 2012)
You Don't Know Query (WordCamp Netherlands 2012)You Don't Know Query (WordCamp Netherlands 2012)
You Don't Know Query (WordCamp Netherlands 2012)
 
Can WordPress really do that? A case study of vierderduer.no
Can WordPress really do that? A case study of vierderduer.noCan WordPress really do that? A case study of vierderduer.no
Can WordPress really do that? A case study of vierderduer.no
 
Caching and Scaling WordPress using Fragment Caching
Caching and Scaling WordPress using Fragment CachingCaching and Scaling WordPress using Fragment Caching
Caching and Scaling WordPress using Fragment Caching
 
Caching in WordPress
Caching in WordPressCaching in WordPress
Caching in WordPress
 
Unit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBeneluxUnit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBenelux
 
Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014
Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014
Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014
 
Temporary Cache Assistance (Transients API): WordCamp Birmingham 2014
Temporary Cache Assistance (Transients API): WordCamp Birmingham 2014Temporary Cache Assistance (Transients API): WordCamp Birmingham 2014
Temporary Cache Assistance (Transients API): WordCamp Birmingham 2014
 
Unit testing with zend framework tek11
Unit testing with zend framework tek11Unit testing with zend framework tek11
Unit testing with zend framework tek11
 
Practical PHP 5.3
Practical PHP 5.3Practical PHP 5.3
Practical PHP 5.3
 
Zend Certification PHP 5 Sample Questions
Zend Certification PHP 5 Sample QuestionsZend Certification PHP 5 Sample Questions
Zend Certification PHP 5 Sample Questions
 

Recently uploaded

一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理
一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理
一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理
aagad
 
Article writing on excessive use of internet.pptx
Article writing on excessive use of internet.pptxArticle writing on excessive use of internet.pptx
Article writing on excessive use of internet.pptx
abhinandnam9997
 

Recently uploaded (12)

The Best AI Powered Software - Intellivid AI Studio
The Best AI Powered Software - Intellivid AI StudioThe Best AI Powered Software - Intellivid AI Studio
The Best AI Powered Software - Intellivid AI Studio
 
The+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptxThe+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptx
 
How Do I Begin the Linksys Velop Setup Process?
How Do I Begin the Linksys Velop Setup Process?How Do I Begin the Linksys Velop Setup Process?
How Do I Begin the Linksys Velop Setup Process?
 
Pvtaan Social media marketing proposal.pdf
Pvtaan Social media marketing proposal.pdfPvtaan Social media marketing proposal.pdf
Pvtaan Social media marketing proposal.pdf
 
The AI Powered Organization-Intro to AI-LAN.pdf
The AI Powered Organization-Intro to AI-LAN.pdfThe AI Powered Organization-Intro to AI-LAN.pdf
The AI Powered Organization-Intro to AI-LAN.pdf
 
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shopHistory+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
 
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and GuidelinesMulti-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
 
The Use of AI in Indonesia Election 2024: A Case Study
The Use of AI in Indonesia Election 2024: A Case StudyThe Use of AI in Indonesia Election 2024: A Case Study
The Use of AI in Indonesia Election 2024: A Case Study
 
一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理
一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理
一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理
 
ER(Entity Relationship) Diagram for online shopping - TAE
ER(Entity Relationship) Diagram for online shopping - TAEER(Entity Relationship) Diagram for online shopping - TAE
ER(Entity Relationship) Diagram for online shopping - TAE
 
Article writing on excessive use of internet.pptx
Article writing on excessive use of internet.pptxArticle writing on excessive use of internet.pptx
Article writing on excessive use of internet.pptx
 
How to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptxHow to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptx
 

Traversing Search Results

  • 2. Tammy Hart Web Designer + Developer at UpTrending @tammyhart
  • 3. Objective: 1. Temporarily store search 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
  • 7. /** * 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; }
  • 9. $wp_session['search_string'] = 'cats' $wp_session['search_hash'] = 'lkmvdsoi939ks0fdm...' $wp_session['search_array'] = array( 2, 4, 8, 9, 11 )
  • 10. /** * 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' );
  • 11. 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.
  • 12. if ( $go_forward ) { return 9 } else { return 4 }
  • 13. /** * 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 ); ...
  • 14. ... // 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]; }
  • 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>