Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Why Hacking WordPress Search Isn't Some Big Scary Thing

1,742 views

Published on

Developers can be a stubborn lot. We like to focus on the things we know really well, and things we don’t deal with as often can be seen as “hard”. Search is one of those things that gets written off a lot, few devs I know will deal with it (voluntarily), and almost everyone will tell you “WordPress search sucks.” In this presentation you will learn that not only do you know more about how search works than you thought you did, but maybe give you a better understanding of how WordPress works as a whole, as well as showing you some cool tricks that you can do with WordPress search.

Published in: Software
  • Be the first to comment

  • Be the first to like this

Why Hacking WordPress Search Isn't Some Big Scary Thing

  1. 1. Why Hacking WordPress Search Isn’t Some Big Scary Thing WordCamp SLC 2014 Chris Reynolds @jazzs3quence
  2. 2. Who’s this guy?
  3. 3. Who’s this guy? WordPress Developer
  4. 4. Who’s this guy? WordPress Developer
  5. 5. Who’s this guy? WordPress Developer
  6. 6. Who’s this guy? WordPress Developer Course Author
  7. 7. 3 misconceptions about search
  8. 8. 3 misconceptions about search 1.“I don’t know anything about search.”
  9. 9. 3 misconceptions about search 1.“I don’t know anything about search.” ?s=some%20query
  10. 10. 3 misconceptions about search
  11. 11. 3 misconceptions about search 2.“WordPress search is hard.”
  12. 12. 3 misconceptions about search
  13. 13. 3 misconceptions about search 3.“WordPress search sucks.”
  14. 14. 3 misconceptions about search 3.“WordPress search sucks.” Does what you ask it to.
  15. 15. 3 misconceptions about search 3.“WordPress search sucks.” Does what you ask it to. No weight given to titles vs. content.
  16. 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. 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. 18. What does WordPress search do? ?s=your%20search
  19. 19. What does WordPress search do? ?s=your%20search
  20. 20. What does WordPress search do? ?s=your%20search ( $wpdb->posts.post_title LIKE %s ) OR ( $wpdb->posts.post_content LIKE %s )
  21. 21. Let me tell you something about query strings and the WordPress Loop
  22. 22. Recognize this? ?p=123
  23. 23. What’s going on? ?p=123
  24. 24. What about this? ?cat=music
  25. 25. What’s going on? ?cat=music
  26. 26. Did you know you can combine these? ?post_type=article&cat=music
  27. 27. What’s going on? ?post_type=article& taxonomy=music&term=jazz
  28. 28. -?p=123
  29. 29. -?p=123 -?paged=2
  30. 30. -?p=123 -?paged=2 -?cat=music
  31. 31. -?p=123 -?paged=2 -?cat=music -?s=your%20search
  32. 32. $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();
  33. 33. $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();
  34. 34. domain.com/? post_type=music&post_status=publish& orderby=date&order=DESC&paged=2&igno re_sticky_posts=1
  35. 35. WordPress search isn’t hard
  36. 36. WordPress search isn’t hard You already know all this stuff
  37. 37. WordPress search isn’t hard You already know all this stuff You just may not know you know it
  38. 38. Ready to have your mind blown?
  39. 39. Ready to have your mind blown? You can add all these query args to search
  40. 40. ?s=your %20search&post_type=article&taxonomy=music &term=jazz
  41. 41. ?s=your %20search&post_type=article&taxonomy=music &term=jazz
  42. 42. How do I actually use this? Like, in a form?
  43. 43. <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>
  44. 44. <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>
  45. 45. Examples!
  46. 46. posts
  47. 47. users posts
  48. 48. users songs posts
  49. 49. users songs posts ?s=patrick&post_type%5B%5D=post&post_type%5B %5D=song&post_type%5B%5D=artist
  50. 50. <?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; ?>
  51. 51. /** * 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
  52. 52. /** * 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' );
  53. 53. /** * 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
  54. 54. only search within current taxonomy
  55. 55. 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; }
  56. 56. 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
  57. 57. Where does that leave us?
  58. 58. Where does that leave us? “I don’t know anything about search.” You do now.
  59. 59. 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.
  60. 60. 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.
  61. 61. Questions?
  62. 62. Questions? Chris Reynolds @jazzs3quence
  63. 63. Questions? Chris Reynolds @jazzs3quence Pluralsight Courses — http://s3q.us/cr-ps
  64. 64. Questions? Chris Reynolds @jazzs3quence Pluralsight Courses — http://s3q.us/cr-ps These slides — http://s3q.us/wcslc2014
  65. 65. 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
  66. 66. 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

×