Victoria wordpress

1,245 views

Published on

A quick demonstration on how you can customize your WordPress sites search functionality. Including how to add relevant search results.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,245
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Discuss what I’ve been developing lately and why it motivated me to do this presentation.
  • Discuss the form as being a default component of WordPress and highlight text input and why its name is important.
  • Victoria wordpress

    1. 1. Focusing Search Improving the built in search functionality of WordPressBy Flynn O’Connor / @thoronas
    2. 2. My name is Flynn O’Connor• Developing WordPress themes for 4years.• Lead developer at Forge and Smith• Slides available here:http://slidesha.re/VTYhJ4
    3. 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. 4. How WordPress Search work?When you use the default WordPress searchform it’s the equivalent of this:$query = new WP_Query( ’s’ => ‘keyword’ );
    5. 5. Take ControlInputs added to the form can control thequery. But they must match thecorresponding query parameters.
    6. 6. Doing it wrongNeither 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. 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 eventsposts.
    8. 8. Give users controlAllow users to search within a singlecategory 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. 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. 10. The resultsWhat your form looks like:What your query string looks like
    11. 11. Form inputs are helpful for adding simplesearch parameters. But for multiple choiceelements we need more control.The following won’t work:$args = array ( ‘custom_tax’ => array(7, 8));$query = new WP_Query( $args );
    12. 12. Say hello to my little friend pre_get_postsThis action will alter the query before it isrun. With this you can do complicatedsearch queries involving taxonomies andmeta values.
    13. 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. 14. Changing the structureIf you allow search for multiple post typessometimes you will need to have differentstructure/layout for your content.
    15. 15. get_post_type( )Use this to load different templates based onthe post types searched.while ( have_posts() ) : the_post();get_template_part( ’content, get_post_type() );endwhile;
    16. 16. The elephant in the roomYou can sort the results :• Date• Name• Menu Order• RandomThe major problem with the built inWordPress search: search results can’t besorted by relevance to the search term.
    17. 17. RelevanssiDefault search results ordered by relevanceinstead of date.
    18. 18. Non relevant search results
    19. 19. Relevant search results
    20. 20. Plays nice with othersBecause we are using pre_get_posts andpassing valid query variables via formelements all of our custom search querieswill work with Relevanssi right out of thebox.
    21. 21. Thanks for listening! Questions/Comments?

    ×