SlideShare uma empresa Scribd logo
1 de 21
Focusing Search

 Improving the built in search
  functionality of WordPress
By Flynn O’Connor / @thoronas
My name is Flynn O’Connor
• Developing WordPress themes for 4
years.
• Lead developer at Forge and Smith
• Slides available here:
http://slidesha.re/VTYhJ4
The basic form
<form method="get" id="searchform” action="<?php echo home_url( '/' ); ?>">

<div>

<label class="screen-reader-text" for="s">Search for:</label>

<input type="text" value="" name="s" id="s" />

<input type="submit" id="searchsubmit" value="Search" />

</div>

</form>
How WordPress Search work?



When you use the default WordPress search
form it’s the equivalent of this:
$query = new WP_Query( ’s’ => ‘keyword’ );
Take Control
Inputs added to the form can control the
query. But they must match the
corresponding query parameters.
Doing it wrong
Neither of these will work automatically:
•   <input name="post-type" type="hidden" value=”listing" />

•   <input name="postType" type="hidden" value=”listing" />




It’s like trying to do:
$query = new WP_Query( ’post-type’ => ‘listing' );
The Right Way
• <input name="post_type" type="hidden" value=”listing" />




Equivalent of:
$query = new WP_Query(
’s’ => ‘keyword’,
‘post_type’ => ‘listing’ );

The Search query is altered to only search events
posts.
Give users control
Allow users to search within a single
category with a WordPress core function.
Example:
<?phpwp_dropdown_categories(); ?>
Outputs:
<select id="cat" name="cat">
<option value="1" class="level-0">Uncategorized</option>
<option value="4" class="level-0">Featured</option>
</select>
Expanding the choices
<?php $search_taxonomy = get_terms( ’custom_taxonomy' );

     if ( !empty( $search_categories ) ) {

foreach( $search_taxonomy as $term) {



               echo ‘<label for=”term_’ . $term->term_id . ‘">
<input type="checkbox" name=”custom_tax[]”
               id=”term_' . $term->term_id . '"
               value="' . $term->term_id . ’”>'
               . $term->name . ‘</label>’;



            }
     } ?>
The results

What your form looks like:




What your query string looks like
Form inputs are helpful for adding simple
search parameters. But for multiple choice
elements we need more control.
The following won’t work:
$args = array (
    ‘custom_tax’ => array(7, 8)
);
$query = new WP_Query( $args );
Say hello to my little friend
                pre_get_posts

This action will alter the query before it is
run. With this you can do complicated
search queries involving taxonomies and
meta values.
function filter_search_terms (){
    if ( isset( $_GET['category'] ) ) {
           global $wp_query;
           $categories = array_map( 'absint', $_GET['category'] );



         $tax_args = array (
             array(
                 'taxonomy' => 'category',
                 'field' => 'id',
                 'terms' => $categories
             )
         );
         $wp_query->set('tax_query', $tax_args);
    }
}

add_action('pre_get_posts','filter_search_terms');
Changing the structure
If you allow search for multiple post types
sometimes you will need to have different
structure/layout for your content.
get_post_type( )
Use this to load different templates based on
the post types searched.

while ( have_posts() ) : the_post();

get_template_part( ’content', get_post_type() );

endwhile;
The elephant in the room
You can sort the results :
• Date
• Name
• Menu Order
• Random
The major problem with the built in
WordPress search: search results can’t be
sorted by relevance to the search term.
Relevanssi




Default search results ordered by relevance
instead of date.
Non relevant search results
Relevant search results
Plays nice with others
Because we are using pre_get_posts and
passing valid query variables via form
elements all of our custom search queries
will work with Relevanssi right out of the
box.
Thanks for listening!

 Questions/Comments?

Mais conteúdo relacionado

Mais procurados

Asp.net templated razor delegates
Asp.net templated razor delegatesAsp.net templated razor delegates
Asp.net templated razor delegates
LearningTech
 
Database presentation
Database presentationDatabase presentation
Database presentation
webhostingguy
 
Coder Presentation Szeged
Coder Presentation SzegedCoder Presentation Szeged
Coder Presentation Szeged
Doug Green
 

Mais procurados (20)

Asp.net templated razor delegates
Asp.net templated razor delegatesAsp.net templated razor delegates
Asp.net templated razor delegates
 
Crafting [Better] API Clients
Crafting [Better] API ClientsCrafting [Better] API Clients
Crafting [Better] API Clients
 
Android - Values folder
Android - Values folderAndroid - Values folder
Android - Values folder
 
06 Php Mysql Connect Query
06 Php Mysql Connect Query06 Php Mysql Connect Query
06 Php Mysql Connect Query
 
Episode 4 - Introduction to SOQL in Salesforce
Episode 4  - Introduction to SOQL in SalesforceEpisode 4  - Introduction to SOQL in Salesforce
Episode 4 - Introduction to SOQL in Salesforce
 
SQL select clause
SQL select clauseSQL select clause
SQL select clause
 
Theming Search Results - How to Make Your Search Results Rock
Theming Search Results - How to Make Your Search Results RockTheming Search Results - How to Make Your Search Results Rock
Theming Search Results - How to Make Your Search Results Rock
 
01 05 - introduction xml
01  05 - introduction xml01  05 - introduction xml
01 05 - introduction xml
 
Database presentation
Database presentationDatabase presentation
Database presentation
 
Flask 소수전공 강의자료 - 3차시
Flask 소수전공 강의자료 - 3차시Flask 소수전공 강의자료 - 3차시
Flask 소수전공 강의자료 - 3차시
 
Coder Presentation Szeged
Coder Presentation SzegedCoder Presentation Szeged
Coder Presentation Szeged
 
Creating Custom Drupal Modules
Creating Custom Drupal ModulesCreating Custom Drupal Modules
Creating Custom Drupal Modules
 
Episode 6 - DML, Transaction and Error handling in Salesforce
Episode 6 - DML, Transaction and Error handling in SalesforceEpisode 6 - DML, Transaction and Error handling in Salesforce
Episode 6 - DML, Transaction and Error handling in Salesforce
 
Drupal Module Development
Drupal Module DevelopmentDrupal Module Development
Drupal Module Development
 
Html To JSP
Html To JSPHtml To JSP
Html To JSP
 
Test driven development with behat and silex
Test driven development with behat and silexTest driven development with behat and silex
Test driven development with behat and silex
 
CodeIgniter L4 file upload & image manipulation & language
CodeIgniter L4 file upload & image manipulation & languageCodeIgniter L4 file upload & image manipulation & language
CodeIgniter L4 file upload & image manipulation & language
 
May the core be with you - JandBeyond 2014
May the core be with you - JandBeyond 2014May the core be with you - JandBeyond 2014
May the core be with you - JandBeyond 2014
 
lab56_db
lab56_dblab56_db
lab56_db
 
Drupal 8 Services And Dependency Injection
Drupal 8 Services And Dependency InjectionDrupal 8 Services And Dependency Injection
Drupal 8 Services And Dependency Injection
 

Semelhante a Victoria wordpress

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
 
Gail villanueva add muscle to your wordpress site
Gail villanueva   add muscle to your wordpress siteGail villanueva   add muscle to your wordpress site
Gail villanueva add muscle to your wordpress site
references
 
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
 

Semelhante a Victoria wordpress (20)

WP_Query, pre_get_posts, and eliminating query_posts()
WP_Query, pre_get_posts, and eliminating query_posts()WP_Query, pre_get_posts, and eliminating query_posts()
WP_Query, pre_get_posts, and eliminating query_posts()
 
Getting Creative with WordPress Queries
Getting Creative with WordPress QueriesGetting Creative with WordPress Queries
Getting Creative with WordPress Queries
 
Unit testing zend framework apps
Unit testing zend framework appsUnit testing zend framework apps
Unit testing zend framework apps
 
Why Hacking WordPress Search Isn't Some Big Scary Thing
Why Hacking WordPress Search Isn't Some Big Scary ThingWhy Hacking WordPress Search Isn't Some Big Scary Thing
Why Hacking WordPress Search Isn't Some Big Scary Thing
 
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
 
[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
 
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
 
Supercharging WordPress Development - Wordcamp Brighton 2019
Supercharging WordPress Development - Wordcamp Brighton 2019Supercharging WordPress Development - Wordcamp Brighton 2019
Supercharging WordPress Development - Wordcamp Brighton 2019
 
The Way to Theme Enlightenment
The Way to Theme EnlightenmentThe Way to Theme Enlightenment
The Way to Theme Enlightenment
 
Working with WP_Query in WordPress
Working with WP_Query in WordPressWorking with WP_Query in WordPress
Working with WP_Query in WordPress
 
Transforming WordPress Search and Query Performance with Elasticsearch
Transforming WordPress Search and Query Performance with Elasticsearch Transforming WordPress Search and Query Performance with Elasticsearch
Transforming WordPress Search and Query Performance with Elasticsearch
 
The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017
 
Custom Post Types and Meta Fields
Custom Post Types and Meta FieldsCustom Post Types and Meta Fields
Custom Post Types and Meta Fields
 
Gail villanueva add muscle to your wordpress site
Gail villanueva   add muscle to your wordpress siteGail villanueva   add muscle to your wordpress site
Gail villanueva add muscle to your wordpress site
 
Simple Usability Tweaks for Your WordPress Theme
Simple Usability Tweaks for Your WordPress ThemeSimple Usability Tweaks for Your WordPress Theme
Simple Usability Tweaks for Your WordPress Theme
 
Unit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBeneluxUnit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBenelux
 
Unit testing with zend framework tek11
Unit testing with zend framework tek11Unit testing with zend framework tek11
Unit testing with zend framework tek11
 
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
 
WordPress Queries - the right way
WordPress Queries - the right wayWordPress Queries - the right way
WordPress Queries - the right way
 
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
 

Último

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Último (20)

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 

Victoria wordpress

  • 1. Focusing Search Improving the built in search functionality of WordPress By Flynn O’Connor / @thoronas
  • 2. My name is Flynn O’Connor • Developing WordPress themes for 4 years. • Lead developer at Forge and Smith • Slides available here: http://slidesha.re/VTYhJ4
  • 3. The basic form <form method="get" id="searchform” action="<?php echo home_url( '/' ); ?>"> <div> <label class="screen-reader-text" for="s">Search for:</label> <input type="text" value="" name="s" id="s" /> <input type="submit" id="searchsubmit" value="Search" /> </div> </form>
  • 4. How WordPress Search work? When you use the default WordPress search form it’s the equivalent of this: $query = new WP_Query( ’s’ => ‘keyword’ );
  • 5. Take Control Inputs added to the form can control the query. But they must match the corresponding query parameters.
  • 6. Doing it wrong Neither of these will work automatically: • <input name="post-type" type="hidden" value=”listing" /> • <input name="postType" type="hidden" value=”listing" /> It’s like trying to do: $query = new WP_Query( ’post-type’ => ‘listing' );
  • 7. The Right Way • <input name="post_type" type="hidden" value=”listing" /> Equivalent of: $query = new WP_Query( ’s’ => ‘keyword’, ‘post_type’ => ‘listing’ ); The Search query is altered to only search events posts.
  • 8. Give users control Allow users to search within a single category with a WordPress core function. Example: <?phpwp_dropdown_categories(); ?> Outputs: <select id="cat" name="cat"> <option value="1" class="level-0">Uncategorized</option> <option value="4" class="level-0">Featured</option> </select>
  • 9. Expanding the choices <?php $search_taxonomy = get_terms( ’custom_taxonomy' ); if ( !empty( $search_categories ) ) { foreach( $search_taxonomy as $term) { echo ‘<label for=”term_’ . $term->term_id . ‘"> <input type="checkbox" name=”custom_tax[]” id=”term_' . $term->term_id . '" value="' . $term->term_id . ’”>' . $term->name . ‘</label>’; } } ?>
  • 10. The results What your form looks like: What your query string looks like
  • 11. Form inputs are helpful for adding simple search parameters. But for multiple choice elements we need more control. The following won’t work: $args = array ( ‘custom_tax’ => array(7, 8) ); $query = new WP_Query( $args );
  • 12. Say hello to my little friend pre_get_posts This action will alter the query before it is run. With this you can do complicated search queries involving taxonomies and meta values.
  • 13. function filter_search_terms (){ if ( isset( $_GET['category'] ) ) { global $wp_query; $categories = array_map( 'absint', $_GET['category'] ); $tax_args = array ( array( 'taxonomy' => 'category', 'field' => 'id', 'terms' => $categories ) ); $wp_query->set('tax_query', $tax_args); } } add_action('pre_get_posts','filter_search_terms');
  • 14. Changing the structure If you allow search for multiple post types sometimes you will need to have different structure/layout for your content.
  • 15. get_post_type( ) Use this to load different templates based on the post types searched. while ( have_posts() ) : the_post(); get_template_part( ’content', get_post_type() ); endwhile;
  • 16. The elephant in the room You can sort the results : • Date • Name • Menu Order • Random The major problem with the built in WordPress search: search results can’t be sorted by relevance to the search term.
  • 17. Relevanssi Default search results ordered by relevance instead of date.
  • 20. Plays nice with others Because we are using pre_get_posts and passing valid query variables via form elements all of our custom search queries will work with Relevanssi right out of the box.
  • 21. Thanks for listening! Questions/Comments?

Notas do Editor

  1. Discuss what I’ve been developing lately and why it motivated me to do this presentation.
  2. Discuss the form as being a default component of WordPress and highlight text input and why its name is important.