SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Mais procurados

(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
Olaf Alders
 
Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB
jhchabran
 
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
Jeremy Kendall
 
Php code for online quiz
Php code for online quizPhp code for online quiz
Php code for online quiz
hnyb1002
 

Mais procurados (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
 

Destaque (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
 

Semelhante a Why Hacking WordPress Search Isn't Some Big Scary Thing

Propel sfugmd
Propel sfugmdPropel sfugmd
Propel sfugmd
iKlaus
 
DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7
chuvainc
 

Semelhante a 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
 

Mais de Chris 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 WPSLC
Chris Reynolds
 

Mais de 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
 

Último

%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
chiefasafspells
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 

Último (20)

Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 

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