SlideShare a Scribd company logo
1 of 74
Why Hacking WordPress 
Search Isn’t Some Big 
Scary Thing 
WordCamp SLC 2014 
Chris Reynolds 
@jazzs3quence
Who’s 
this guy?
Who’s 
this guy? 
WordPress Developer
Who’s 
this guy? 
WordPress Developer
Who’s 
this guy? 
WordPress Developer
Who’s 
this guy? 
WordPress Developer 
Course Author
3 misconceptions 
about search
3 misconceptions 
about search 
1.“I don’t know anything about 
search.”
3 misconceptions 
about search 
1.“I don’t know anything about 
search.” 
?s=some%20query
3 misconceptions 
about search
3 misconceptions 
about search 
2.“WordPress search is hard.”
3 misconceptions 
about search
3 misconceptions 
about search 
3.“WordPress search sucks.”
3 misconceptions 
about search 
3.“WordPress search sucks.” 
Does what you ask it to.
3 misconceptions 
about search 
3.“WordPress search sucks.” 
Does what you ask it to. 
No weight given to titles vs. content.
3 misconceptions 
about search 
3.“WordPress search sucks.” 
Does what you ask it to. 
No weight given to titles vs. content. 
You can do a lot of cool stuff with 
search…
3 misconceptions 
about search 
3.“WordPress search sucks.” 
Does what you ask it to. 
No weight given to titles vs. content. 
You can do a lot of cool stuff with 
search… 
(if you know how)
What does WordPress 
search do? 
?s=your%20search
What does WordPress 
search do? 
?s=your%20search
What does WordPress 
search do? 
?s=your%20search 
( $wpdb->posts.post_title LIKE %s ) OR 
( $wpdb->posts.post_content LIKE %s )
Let me tell you 
something about 
query strings and 
the WordPress Loop
Recognize this? 
?p=123
What’s going 
on? 
?p=123
What about 
this? 
?cat=music
What’s going 
on? 
?cat=music
Did you know you 
can combine these? 
?post_type=article&cat=music
What’s going 
on? 
?post_type=article& 
taxonomy=music&term=jazz
-?p=123
-?p=123 
-?paged=2
-?p=123 
-?paged=2 
-?cat=music
-?p=123 
-?paged=2 
-?cat=music 
-?s=your%20search
$args = array( 
'post_type' => 'music', 
'paged' => $paged, 
'orderby' => 'date', 
'order' => 'DESC', 
'post_status' => 'publish', 
'ignore_sticky_posts' => 1 
); 
! 
$my_query = new WP_Query( $args ); 
! 
if ( $my_query->have_posts() ) : 
while ( $my_query->have_posts() ) : 
the_post();
$my_query = new 
WP_Query( 'post_type=music&post_stat 
us=publish&orderby=date&order=DESC&p 
aged=' . $paged . 
'&ignore_sticky_posts=1'); 
! 
if ( $my_query->have_posts() ) : 
while ( $my_query->have_posts() ) : 
the_post();
domain.com/? 
post_type=music&post_status=publish& 
orderby=date&order=DESC&paged=2&igno 
re_sticky_posts=1
WordPress search 
isn’t hard
WordPress search 
isn’t hard 
You already know all this stuff
WordPress search 
isn’t hard 
You already know all this stuff 
You just may not know you know it
Ready to have your mind 
blown?
Ready to have your mind 
blown? 
You can add all these query args to search
?s=your 
%20search&post_type=article&taxonomy=music 
&term=jazz
?s=your 
%20search&post_type=article&taxonomy=music 
&term=jazz
How do I actually 
use this? Like, in 
a form?
<form role="search" method="get" class="search-form" 
action="' . get_home_url( '/' ) . '"> 
<label> 
<input type="search" class="search-field" 
placeholder="Search" value="" name="s" 
title="Search for:" /> 
</label> 
<input type="hidden" name="music" value="jazz" /> 
<input type="hidden" name="post_type" value="article" 
/> 
</form>
<form role="search" method="get" class="search-form" 
action="' . get_home_url( '/' ) . '"> 
<label> 
<input type="search" class="search-field" 
placeholder="Search" value="" name="s" 
title="Search for:" /> 
</label> 
<input type="hidden" name="music" value="jazz" /> 
<input type="hidden" name="post_type" value="article" 
/> 
</form>
Examples!
posts
users posts
users songs posts
users songs posts 
?s=patrick&post_type%5B%5D=post&post_type%5B 
%5D=song&post_type%5B%5D=artist
<?php 
/** 
* If users exist, deal with displaying them first 
*/ 
! 
if ( curated_have_users() && curated_show_users() ) : 
! 
// get the classes to display user results 
$classes = curated_get_grid_classes( $i, 4 ); ?> 
! 
<div id="user-<?php echo sanitize_title( get_search_query() ); ?>" 
class="tile-wrap <?php echo esc_attr( $classes ); ?> "> 
! 
<?php get_template_part( 'partials/search', 'user' ); ?> 
! 
</div> 
! 
<?php 
// increment $i 
$i++; 
endif; ?>
/** 
* set up an array of arguments to determine post2post relationships 
* this stuff gets set up too late to be used in pre_get_posts so I have to use query_posts. 
*/ 
$search_term = curated_posts_like_title( get_search_query() ); 
$query_args = array( 
'connected_type' => 'songs_to_artists', 
'connected_items' => $search_term, 
'connected_direction' => 'any', 
'nopaging' => true, 
// make sure this post isn't a duplicate 
'post__not_in' => $do_not_duplicate 
); 
if ( curated_has_connected_posts( $search_term ) ) { 
// only run the query posts if we need it 
query_posts( $query_args ); 
// sometimes query_posts will break and return nothing even if something should be returned. If this 
happens, reset the query and move on 
if ( !have_posts() ) { wp_reset_query(); } 
} 
while ( have_posts() ) : the_post(); 
$do_not_duplicate[] = $post->ID; // add this post to the duplicate array so we don't get duplicates
/** 
* Run the loop for the search to output the results. 
* If you want to overload this in a child theme then include a file 
* called content-search.php and that will be used instead. 
*/ 
// don't display the other results if we are only looking at users 
if ( 'user' != curated_get_search_selected() && 'artist' != get_post_type() ) { 
if ( '' == $classes ) { 
// if classes haven't been set yet, setup the classes 
$classes = curated_get_grid_classes( $i, 4 ); 
} ?> 
<div id="<?php echo get_post_type(); ?>-<?php echo $post->ID; ?>" class="tile-wrap <?php 
echo get_post_type(); ?> <?php echo esc_attr( $classes ); ?>"> 
<?php get_template_part( 'partials/search', get_post_type() ); ?> 
</div> 
<?php } 
wp_reset_query(); 
$i++; // increment the counter again 
endwhile; 
curated_paging_nav(); 
else : 
get_template_part( 'content', 'none' );
/** 
* Run the loop for the search to output the results. 
* If you want to overload this in a child theme then include a file 
* called content-search.php and that will be used instead. 
*/ 
// don't display the other results if we are only looking at users 
if ( 'user' != curated_get_search_selected() && 'artist' != get_post_type() ) { 
if ( '' == $classes ) { 
// if classes haven't been set yet, setup the classes 
$classes = curated_get_grid_classes( $i, 4 ); 
} ?> 
<div id="<?php echo get_post_type(); ?>-<?php echo $post->ID; ?>" class="tile-wrap <?php 
echo get_post_type(); ?> <?php echo esc_attr( $classes ); ?>"> 
<?php get_template_part( 'partials/search', get_post_type() ); ?> 
</div> 
<?php } 
wp_reset_query(); 
$i++; // increment the counter again 
endwhile; 
curated_paging_nav(); 
else : 
get_template_part( 'content', 'none' ); 
http://s3q.us/ 
wcslc2014-cs-search
only search within current taxonomy
function nps_get_taxonomy_search_form( $term = null, $taxonomy = null ) { 
! 
if ( !$term ) { 
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); 
$term = $term->name; 
} 
! 
if ( !$taxonomy ) 
$taxonomy = get_query_var( 'taxonomy' ); 
! 
$form =' 
<form role="search" method="get" class="search-form" action="' . get_home_url( '/' ) . '"> 
<label> 
<input type="search" class="search-field" placeholder="Search" value="" name="s" 
title="Search for:" /> 
</label> 
<input type="hidden" name="' . $taxonomy . '" value="' . $term . '" /> 
</form> 
'; 
! 
echo $form; 
}
function nps_get_taxonomy_search_form( $term = null, $taxonomy = null ) { 
! 
if ( !$term ) { 
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); 
$term = $term->name; 
} 
! 
if ( !$taxonomy ) 
$taxonomy = get_query_var( 'taxonomy' ); 
! 
$form =' 
<form role="search" method="get" class="search-form" action="' . get_home_url( '/' ) . '"> 
<label> 
<input type="search" class="search-field" placeholder="Search" value="" name="s" 
title="Search for:" /> 
</label> 
<input type="hidden" name="' . $taxonomy . '" value="' . $term . '" /> 
</form> 
'; 
! 
echo $form; 
} 
http://s3q.us/wcslc2014- 
nps-searchform
Where does that 
leave us?
Where does that 
leave us? 
“I don’t know anything about search.” 
You do now.
Where does that 
leave us? 
“I don’t know anything about search.” 
You do now. 
“WordPress search is hard.” 
Not any more so than normal queries.
Where does that 
leave us? 
“I don’t know anything about search.” 
You do now. 
“WordPress search is hard.” 
Not any more so than normal queries. 
“WordPress search sucks.” 
No, it doesn’t.
Questions?
Questions? 
Chris Reynolds 
@jazzs3quence
Questions? 
Chris Reynolds 
@jazzs3quence 
Pluralsight Courses — http://s3q.us/cr-ps
Questions? 
Chris Reynolds 
@jazzs3quence 
Pluralsight Courses — http://s3q.us/cr-ps 
These slides — http://s3q.us/wcslc2014
Questions? 
Chris Reynolds 
@jazzs3quence 
Pluralsight Courses — http://s3q.us/cr-ps 
These slides — http://s3q.us/wcslc2014 
Search form gist — http://s3q.us/wcslc2014-nps-searchform
Questions? 
Chris Reynolds 
@jazzs3quence 
Pluralsight Courses — http://s3q.us/cr-ps 
These slides — http://s3q.us/wcslc2014 
Search form gist — http://s3q.us/wcslc2014-nps-searchform 
Search page gist — http://s3q.us/wcslc2014-cs-search

More Related Content

What's hot

WordPress 3.1 at DC PHP
WordPress 3.1 at DC PHPWordPress 3.1 at DC PHP
WordPress 3.1 at DC PHPandrewnacin
 
(Ab)Using the MetaCPAN API for Fun and Profit
(Ab)Using the MetaCPAN API for Fun and Profit(Ab)Using the MetaCPAN API for Fun and Profit
(Ab)Using the MetaCPAN API for Fun and ProfitOlaf Alders
 
Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB jhchabran
 
Taking Perl to Eleven with Higher-Order Functions
Taking Perl to Eleven with Higher-Order FunctionsTaking Perl to Eleven with Higher-Order Functions
Taking Perl to Eleven with Higher-Order FunctionsDavid Golden
 
Transparent Object Persistence with FLOW3
Transparent Object Persistence with FLOW3Transparent Object Persistence with FLOW3
Transparent Object Persistence with FLOW3Karsten Dambekalns
 
Bag Of Tricks From Iusethis
Bag Of Tricks From IusethisBag Of Tricks From Iusethis
Bag Of Tricks From IusethisMarcus Ramberg
 
FamilySearch Reference Client
FamilySearch Reference ClientFamilySearch Reference Client
FamilySearch Reference ClientDallan Quass
 
HirshHorn theme: how I created it
HirshHorn theme: how I created itHirshHorn theme: how I created it
HirshHorn theme: how I created itPaul Bearne
 
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 queryl3rady
 
Php 102: Out with the Bad, In with the Good
Php 102: Out with the Bad, In with the GoodPhp 102: Out with the Bad, In with the Good
Php 102: Out with the Bad, In with the GoodJeremy Kendall
 
WordPress Cuztom Helper
WordPress Cuztom HelperWordPress Cuztom Helper
WordPress Cuztom Helperslicejack
 
Leveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHPLeveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHPJeremy Kendall
 
Leveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHPLeveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHPJeremy Kendall
 
Php code for online quiz
Php code for online quizPhp code for online quiz
Php code for online quizhnyb1002
 

What's hot (20)

WordPress 3.1 at DC PHP
WordPress 3.1 at DC PHPWordPress 3.1 at DC PHP
WordPress 3.1 at DC PHP
 
(Ab)Using the MetaCPAN API for Fun and Profit
(Ab)Using the MetaCPAN API for Fun and Profit(Ab)Using the MetaCPAN API for Fun and Profit
(Ab)Using the MetaCPAN API for Fun and Profit
 
Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB
 
Wsomdp
WsomdpWsomdp
Wsomdp
 
Taking Perl to Eleven with Higher-Order Functions
Taking Perl to Eleven with Higher-Order FunctionsTaking Perl to Eleven with Higher-Order Functions
Taking Perl to Eleven with Higher-Order Functions
 
Sk.php
Sk.phpSk.php
Sk.php
 
Transparent Object Persistence with FLOW3
Transparent Object Persistence with FLOW3Transparent Object Persistence with FLOW3
Transparent Object Persistence with FLOW3
 
Bag Of Tricks From Iusethis
Bag Of Tricks From IusethisBag Of Tricks From Iusethis
Bag Of Tricks From Iusethis
 
FamilySearch Reference Client
FamilySearch Reference ClientFamilySearch Reference Client
FamilySearch Reference Client
 
wget.pl
wget.plwget.pl
wget.pl
 
HirshHorn theme: how I created it
HirshHorn theme: how I created itHirshHorn theme: how I created it
HirshHorn theme: how I created it
 
Inc
IncInc
Inc
 
PHP 5.4
PHP 5.4PHP 5.4
PHP 5.4
 
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
 
Php 102: Out with the Bad, In with the Good
Php 102: Out with the Bad, In with the GoodPhp 102: Out with the Bad, In with the Good
Php 102: Out with the Bad, In with the Good
 
WordPress Cuztom Helper
WordPress Cuztom HelperWordPress Cuztom Helper
WordPress Cuztom Helper
 
Leveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHPLeveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHP
 
Leveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHPLeveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHP
 
Php code for online quiz
Php code for online quizPhp code for online quiz
Php code for online quiz
 
DBI
DBIDBI
DBI
 

Viewers also liked

Post and page in word press
Post and page in word pressPost and page in word press
Post and page in word pressLucky Ali
 
Mastering Custom Post Types - WordCamp Atlanta 2012
Mastering Custom Post Types - WordCamp Atlanta 2012Mastering Custom Post Types - WordCamp Atlanta 2012
Mastering Custom Post Types - WordCamp Atlanta 2012Mike Schinkel
 
WordPress theme setting page
WordPress theme setting pageWordPress theme setting page
WordPress theme setting pageNaeem Junejo
 
Relationships Between WordPress Post Types
Relationships Between WordPress Post TypesRelationships Between WordPress Post Types
Relationships Between WordPress Post Typesrandyhoyt
 
WordPress & Custm Post Types
WordPress & Custm Post TypesWordPress & Custm Post Types
WordPress & Custm Post TypesDinis Correia
 
WordPress 3 Custom Post Types
WordPress 3 Custom Post TypesWordPress 3 Custom Post Types
WordPress 3 Custom Post TypesDave Zille
 
WordPress Webinar Training Presentation
WordPress Webinar Training PresentationWordPress Webinar Training Presentation
WordPress Webinar Training PresentationMayeCreate Design
 

Viewers also liked (7)

Post and page in word press
Post and page in word pressPost and page in word press
Post and page in word press
 
Mastering Custom Post Types - WordCamp Atlanta 2012
Mastering Custom Post Types - WordCamp Atlanta 2012Mastering Custom Post Types - WordCamp Atlanta 2012
Mastering Custom Post Types - WordCamp Atlanta 2012
 
WordPress theme setting page
WordPress theme setting pageWordPress theme setting page
WordPress theme setting page
 
Relationships Between WordPress Post Types
Relationships Between WordPress Post TypesRelationships Between WordPress Post Types
Relationships Between WordPress Post Types
 
WordPress & Custm Post Types
WordPress & Custm Post TypesWordPress & Custm Post Types
WordPress & Custm Post Types
 
WordPress 3 Custom Post Types
WordPress 3 Custom Post TypesWordPress 3 Custom Post Types
WordPress 3 Custom Post Types
 
WordPress Webinar Training Presentation
WordPress Webinar Training PresentationWordPress Webinar Training Presentation
WordPress Webinar Training Presentation
 

Similar to Why Hacking WordPress Search Isn't Some Big Scary Thing

You code sucks, let's fix it
You code sucks, let's fix itYou code sucks, let's fix it
You code sucks, let's fix itRafael Dohms
 
Your code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnConYour code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnConRafael Dohms
 
Unit testing with zend framework tek11
Unit testing with zend framework tek11Unit testing with zend framework tek11
Unit testing with zend framework tek11Michelangelo van Dam
 
Propel sfugmd
Propel sfugmdPropel sfugmd
Propel sfugmdiKlaus
 
Unit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBeneluxUnit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBeneluxMichelangelo van Dam
 
DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7chuvainc
 
Beyond Posts & Pages - Structured Content in WordPress
Beyond Posts & Pages - Structured Content in WordPressBeyond Posts & Pages - Structured Content in WordPress
Beyond Posts & Pages - Structured Content in WordPressJohn Eckman
 
Adventures in Optimization
Adventures in OptimizationAdventures in Optimization
Adventures in OptimizationDavid Golden
 
Your code sucks, let's fix it - PHP Master Series 2012
Your code sucks, let's fix it - PHP Master Series 2012Your code sucks, let's fix it - PHP Master Series 2012
Your code sucks, let's fix it - PHP Master Series 2012Rafael Dohms
 
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.noMorten Rand-Hendriksen
 
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 2011andrewnacin
 
Your code sucks, let's fix it
Your code sucks, let's fix itYour code sucks, let's fix it
Your code sucks, let's fix itRafael Dohms
 

Similar to Why Hacking WordPress Search Isn't Some Big Scary Thing (20)

Php
PhpPhp
Php
 
Wp query
Wp queryWp query
Wp query
 
You code sucks, let's fix it
You code sucks, let's fix itYou code sucks, let's fix it
You code sucks, let's fix it
 
Your code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnConYour code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnCon
 
Unit testing with zend framework tek11
Unit testing with zend framework tek11Unit testing with zend framework tek11
Unit testing with zend framework tek11
 
Lithium Best
Lithium Best Lithium Best
Lithium Best
 
Victoria wordpress
Victoria wordpressVictoria wordpress
Victoria wordpress
 
Unit testing zend framework apps
Unit testing zend framework appsUnit testing zend framework apps
Unit testing zend framework apps
 
Propel sfugmd
Propel sfugmdPropel sfugmd
Propel sfugmd
 
Unit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBeneluxUnit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBenelux
 
Intro to The PHP SPL
Intro to The PHP SPLIntro to The PHP SPL
Intro to The PHP SPL
 
DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7
 
OOP in PHP.pptx
OOP in PHP.pptxOOP in PHP.pptx
OOP in PHP.pptx
 
Beyond Posts & Pages - Structured Content in WordPress
Beyond Posts & Pages - Structured Content in WordPressBeyond Posts & Pages - Structured Content in WordPress
Beyond Posts & Pages - Structured Content in WordPress
 
Adventures in Optimization
Adventures in OptimizationAdventures in Optimization
Adventures in Optimization
 
Your code sucks, let's fix it - PHP Master Series 2012
Your code sucks, let's fix it - PHP Master Series 2012Your code sucks, let's fix it - PHP Master Series 2012
Your code sucks, let's fix it - PHP Master Series 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
 
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
 
21. CodeIgniter search
21. CodeIgniter search21. CodeIgniter search
21. CodeIgniter search
 
Your code sucks, let's fix it
Your code sucks, let's fix itYour code sucks, let's fix it
Your code sucks, let's fix it
 

More from Chris Reynolds

Developing an SDK for Personalization at the Edge
Developing an SDK for Personalization at the EdgeDeveloping an SDK for Personalization at the Edge
Developing an SDK for Personalization at the EdgeChris Reynolds
 
Outcomes vs Outputs: How Outcome Driven Development Planning Changes Everything
Outcomes vs Outputs: How Outcome Driven Development Planning Changes EverythingOutcomes vs Outputs: How Outcome Driven Development Planning Changes Everything
Outcomes vs Outputs: How Outcome Driven Development Planning Changes EverythingChris Reynolds
 
How the WordPress Block Editor Changes the Conversation for Content Editors a...
How the WordPress Block Editor Changes the Conversation for Content Editors a...How the WordPress Block Editor Changes the Conversation for Content Editors a...
How the WordPress Block Editor Changes the Conversation for Content Editors a...Chris Reynolds
 
How the WordPress Block Editor Changes the Conversation for Content Editors a...
How the WordPress Block Editor Changes the Conversation for Content Editors a...How the WordPress Block Editor Changes the Conversation for Content Editors a...
How the WordPress Block Editor Changes the Conversation for Content Editors a...Chris Reynolds
 
Who's afraid of the big bad loop?
Who's afraid of the big bad loop?Who's afraid of the big bad loop?
Who's afraid of the big bad loop?Chris Reynolds
 
Drop Kick Imposter Syndrome
Drop Kick Imposter SyndromeDrop Kick Imposter Syndrome
Drop Kick Imposter SyndromeChris Reynolds
 
Awesome Git Workflow for Agencies and Teams
Awesome Git Workflow for Agencies and TeamsAwesome Git Workflow for Agencies and Teams
Awesome Git Workflow for Agencies and TeamsChris Reynolds
 
9 Things You Didn't Know You Could Do with Your Blog WPSLC
9 Things You Didn't Know You Could Do with Your Blog WPSLC9 Things You Didn't Know You Could Do with Your Blog WPSLC
9 Things You Didn't Know You Could Do with Your Blog WPSLCChris Reynolds
 
9 things you didn't know you could do with your blog
9 things you didn't know you could do with your blog9 things you didn't know you could do with your blog
9 things you didn't know you could do with your blogChris Reynolds
 

More from Chris Reynolds (10)

Developing an SDK for Personalization at the Edge
Developing an SDK for Personalization at the EdgeDeveloping an SDK for Personalization at the Edge
Developing an SDK for Personalization at the Edge
 
Outcomes vs Outputs: How Outcome Driven Development Planning Changes Everything
Outcomes vs Outputs: How Outcome Driven Development Planning Changes EverythingOutcomes vs Outputs: How Outcome Driven Development Planning Changes Everything
Outcomes vs Outputs: How Outcome Driven Development Planning Changes Everything
 
How the WordPress Block Editor Changes the Conversation for Content Editors a...
How the WordPress Block Editor Changes the Conversation for Content Editors a...How the WordPress Block Editor Changes the Conversation for Content Editors a...
How the WordPress Block Editor Changes the Conversation for Content Editors a...
 
How the WordPress Block Editor Changes the Conversation for Content Editors a...
How the WordPress Block Editor Changes the Conversation for Content Editors a...How the WordPress Block Editor Changes the Conversation for Content Editors a...
How the WordPress Block Editor Changes the Conversation for Content Editors a...
 
Who's afraid of the big bad loop?
Who's afraid of the big bad loop?Who's afraid of the big bad loop?
Who's afraid of the big bad loop?
 
Being a better ally
Being a better allyBeing a better ally
Being a better ally
 
Drop Kick Imposter Syndrome
Drop Kick Imposter SyndromeDrop Kick Imposter Syndrome
Drop Kick Imposter Syndrome
 
Awesome Git Workflow for Agencies and Teams
Awesome Git Workflow for Agencies and TeamsAwesome Git Workflow for Agencies and Teams
Awesome Git Workflow for Agencies and Teams
 
9 Things You Didn't Know You Could Do with Your Blog WPSLC
9 Things You Didn't Know You Could Do with Your Blog WPSLC9 Things You Didn't Know You Could Do with Your Blog WPSLC
9 Things You Didn't Know You Could Do with Your Blog WPSLC
 
9 things you didn't know you could do with your blog
9 things you didn't know you could do with your blog9 things you didn't know you could do with your blog
9 things you didn't know you could do with your blog
 

Recently uploaded

What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxAndreas Kunz
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxReal-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxRTS corp
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecturerahul_net
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...Akihiro Suda
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLionel Briand
 

Recently uploaded (20)

What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxReal-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecture
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and Repair
 

Why Hacking WordPress Search Isn't Some Big Scary Thing

  • 1. Why Hacking WordPress Search Isn’t Some Big Scary Thing WordCamp SLC 2014 Chris Reynolds @jazzs3quence
  • 3. Who’s this guy? WordPress Developer
  • 4. Who’s this guy? WordPress Developer
  • 5. Who’s this guy? WordPress Developer
  • 6. Who’s this guy? WordPress Developer Course Author
  • 8. 3 misconceptions about search 1.“I don’t know anything about search.”
  • 9. 3 misconceptions about search 1.“I don’t know anything about search.” ?s=some%20query
  • 11. 3 misconceptions about search 2.“WordPress search is hard.”
  • 13. 3 misconceptions about search 3.“WordPress search sucks.”
  • 14. 3 misconceptions about search 3.“WordPress search sucks.” Does what you ask it to.
  • 15. 3 misconceptions about search 3.“WordPress search sucks.” Does what you ask it to. No weight given to titles vs. content.
  • 16. 3 misconceptions about search 3.“WordPress search sucks.” Does what you ask it to. No weight given to titles vs. content. You can do a lot of cool stuff with search…
  • 17. 3 misconceptions about search 3.“WordPress search sucks.” Does what you ask it to. No weight given to titles vs. content. You can do a lot of cool stuff with search… (if you know how)
  • 18. What does WordPress search do? ?s=your%20search
  • 19. What does WordPress search do? ?s=your%20search
  • 20. What does WordPress search do? ?s=your%20search ( $wpdb->posts.post_title LIKE %s ) OR ( $wpdb->posts.post_content LIKE %s )
  • 21. Let me tell you something about query strings and the WordPress Loop
  • 24. What about this? ?cat=music
  • 25. What’s going on? ?cat=music
  • 26. Did you know you can combine these? ?post_type=article&cat=music
  • 27. What’s going on? ?post_type=article& taxonomy=music&term=jazz
  • 28.
  • 32. -?p=123 -?paged=2 -?cat=music -?s=your%20search
  • 33.
  • 34. $args = array( 'post_type' => 'music', 'paged' => $paged, 'orderby' => 'date', 'order' => 'DESC', 'post_status' => 'publish', 'ignore_sticky_posts' => 1 ); ! $my_query = new WP_Query( $args ); ! if ( $my_query->have_posts() ) : while ( $my_query->have_posts() ) : the_post();
  • 35. $my_query = new WP_Query( 'post_type=music&post_stat us=publish&orderby=date&order=DESC&p aged=' . $paged . '&ignore_sticky_posts=1'); ! if ( $my_query->have_posts() ) : while ( $my_query->have_posts() ) : the_post();
  • 38. WordPress search isn’t hard You already know all this stuff
  • 39. WordPress search isn’t hard You already know all this stuff You just may not know you know it
  • 40.
  • 41. Ready to have your mind blown?
  • 42. Ready to have your mind blown? You can add all these query args to search
  • 43.
  • 44.
  • 47. How do I actually use this? Like, in a form?
  • 48. <form role="search" method="get" class="search-form" action="' . get_home_url( '/' ) . '"> <label> <input type="search" class="search-field" placeholder="Search" value="" name="s" title="Search for:" /> </label> <input type="hidden" name="music" value="jazz" /> <input type="hidden" name="post_type" value="article" /> </form>
  • 49. <form role="search" method="get" class="search-form" action="' . get_home_url( '/' ) . '"> <label> <input type="search" class="search-field" placeholder="Search" value="" name="s" title="Search for:" /> </label> <input type="hidden" name="music" value="jazz" /> <input type="hidden" name="post_type" value="article" /> </form>
  • 51.
  • 52. posts
  • 55. users songs posts ?s=patrick&post_type%5B%5D=post&post_type%5B %5D=song&post_type%5B%5D=artist
  • 56. <?php /** * If users exist, deal with displaying them first */ ! if ( curated_have_users() && curated_show_users() ) : ! // get the classes to display user results $classes = curated_get_grid_classes( $i, 4 ); ?> ! <div id="user-<?php echo sanitize_title( get_search_query() ); ?>" class="tile-wrap <?php echo esc_attr( $classes ); ?> "> ! <?php get_template_part( 'partials/search', 'user' ); ?> ! </div> ! <?php // increment $i $i++; endif; ?>
  • 57. /** * set up an array of arguments to determine post2post relationships * this stuff gets set up too late to be used in pre_get_posts so I have to use query_posts. */ $search_term = curated_posts_like_title( get_search_query() ); $query_args = array( 'connected_type' => 'songs_to_artists', 'connected_items' => $search_term, 'connected_direction' => 'any', 'nopaging' => true, // make sure this post isn't a duplicate 'post__not_in' => $do_not_duplicate ); if ( curated_has_connected_posts( $search_term ) ) { // only run the query posts if we need it query_posts( $query_args ); // sometimes query_posts will break and return nothing even if something should be returned. If this happens, reset the query and move on if ( !have_posts() ) { wp_reset_query(); } } while ( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID; // add this post to the duplicate array so we don't get duplicates
  • 58. /** * Run the loop for the search to output the results. * If you want to overload this in a child theme then include a file * called content-search.php and that will be used instead. */ // don't display the other results if we are only looking at users if ( 'user' != curated_get_search_selected() && 'artist' != get_post_type() ) { if ( '' == $classes ) { // if classes haven't been set yet, setup the classes $classes = curated_get_grid_classes( $i, 4 ); } ?> <div id="<?php echo get_post_type(); ?>-<?php echo $post->ID; ?>" class="tile-wrap <?php echo get_post_type(); ?> <?php echo esc_attr( $classes ); ?>"> <?php get_template_part( 'partials/search', get_post_type() ); ?> </div> <?php } wp_reset_query(); $i++; // increment the counter again endwhile; curated_paging_nav(); else : get_template_part( 'content', 'none' );
  • 59. /** * Run the loop for the search to output the results. * If you want to overload this in a child theme then include a file * called content-search.php and that will be used instead. */ // don't display the other results if we are only looking at users if ( 'user' != curated_get_search_selected() && 'artist' != get_post_type() ) { if ( '' == $classes ) { // if classes haven't been set yet, setup the classes $classes = curated_get_grid_classes( $i, 4 ); } ?> <div id="<?php echo get_post_type(); ?>-<?php echo $post->ID; ?>" class="tile-wrap <?php echo get_post_type(); ?> <?php echo esc_attr( $classes ); ?>"> <?php get_template_part( 'partials/search', get_post_type() ); ?> </div> <?php } wp_reset_query(); $i++; // increment the counter again endwhile; curated_paging_nav(); else : get_template_part( 'content', 'none' ); http://s3q.us/ wcslc2014-cs-search
  • 60.
  • 61.
  • 62. only search within current taxonomy
  • 63. function nps_get_taxonomy_search_form( $term = null, $taxonomy = null ) { ! if ( !$term ) { $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); $term = $term->name; } ! if ( !$taxonomy ) $taxonomy = get_query_var( 'taxonomy' ); ! $form =' <form role="search" method="get" class="search-form" action="' . get_home_url( '/' ) . '"> <label> <input type="search" class="search-field" placeholder="Search" value="" name="s" title="Search for:" /> </label> <input type="hidden" name="' . $taxonomy . '" value="' . $term . '" /> </form> '; ! echo $form; }
  • 64. function nps_get_taxonomy_search_form( $term = null, $taxonomy = null ) { ! if ( !$term ) { $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); $term = $term->name; } ! if ( !$taxonomy ) $taxonomy = get_query_var( 'taxonomy' ); ! $form =' <form role="search" method="get" class="search-form" action="' . get_home_url( '/' ) . '"> <label> <input type="search" class="search-field" placeholder="Search" value="" name="s" title="Search for:" /> </label> <input type="hidden" name="' . $taxonomy . '" value="' . $term . '" /> </form> '; ! echo $form; } http://s3q.us/wcslc2014- nps-searchform
  • 65. Where does that leave us?
  • 66. Where does that leave us? “I don’t know anything about search.” You do now.
  • 67. Where does that leave us? “I don’t know anything about search.” You do now. “WordPress search is hard.” Not any more so than normal queries.
  • 68. Where does that leave us? “I don’t know anything about search.” You do now. “WordPress search is hard.” Not any more so than normal queries. “WordPress search sucks.” No, it doesn’t.
  • 70. Questions? Chris Reynolds @jazzs3quence
  • 71. Questions? Chris Reynolds @jazzs3quence Pluralsight Courses — http://s3q.us/cr-ps
  • 72. Questions? Chris Reynolds @jazzs3quence Pluralsight Courses — http://s3q.us/cr-ps These slides — http://s3q.us/wcslc2014
  • 73. Questions? Chris Reynolds @jazzs3quence Pluralsight Courses — http://s3q.us/cr-ps These slides — http://s3q.us/wcslc2014 Search form gist — http://s3q.us/wcslc2014-nps-searchform
  • 74. Questions? Chris Reynolds @jazzs3quence Pluralsight Courses — http://s3q.us/cr-ps These slides — http://s3q.us/wcslc2014 Search form gist — http://s3q.us/wcslc2014-nps-searchform Search page gist — http://s3q.us/wcslc2014-cs-search