Changeset 11606
- Timestamp:
- 02/25/2022 04:23:33 AM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.tv/public_html/wp-content/themes/wptv2/functions.php
r10466 r11606 30 30 add_action( 'init', array( $this, 'register_taxonomies' ), 0 ); 31 31 add_action( 'pre_get_posts', array( $this, 'posts_per_page' ) ); 32 add_action( 'init', array( $this, 'improve_search' ) );33 32 add_action( 'publish_post', array( $this, 'publish_post' ), 10, 1 ); 34 33 add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) ); … … 223 222 */ 224 223 225 $data[] = array_filter( $video_data, function( $item ) { 224 $data[] = array_filter( $video_data, function( $item ) { 226 225 return '' !== $item && [] !== $item; 227 226 } ); … … 414 413 } 415 414 416 /**417 * Activates the improved search, but not in admin.418 */419 function improve_search() {420 if ( is_admin() ) {421 return;422 }423 424 add_filter( 'posts_search', array( $this, 'search_posts_search' ), 10, 2 );425 add_action( 'pre_get_posts', array( $this, 'search_pre_get_posts' ) );426 }427 428 /**429 * Sort the search results by the number of views each post has430 *431 * @param WP_Query $query432 */433 function search_pre_get_posts( $query ) {434 /*435 * @todo Optimize this before re-enabling436 *437 * This method was disabled because it caused 504 errors on large result sets438 * (e.g., https://wordpress.tv/?s=keynote). Sorting by a meta value is not performant.439 *440 * Maybe look at ways to do the sorting in PHP, or just use Elasticsearch instead.441 */442 return;443 444 if ( ! $query->is_main_query() || ! $query->is_search ) {445 return;446 }447 448 // Set custom sorting449 $query->set( 'meta_key', 'wptv_post_views' );450 $query->set( 'orderby', 'meta_value_num' );451 $query->set( 'order', 'DESC' );452 }453 454 /**455 * Improved Search: posts_search filter456 *457 * Recreates the search SQL by including a taxonomy search.458 * Relies on various other filters used once.459 * @todo optimize the get_tax_query part.460 *461 * @global wpdb $wpdb WordPress database abstraction object.462 *463 * @param string $search464 * @param WP_Query $query465 * @return string466 */467 function search_posts_search( $search, $query ) {468 global $wpdb;469 if ( ! $query->is_main_query() || ! $query->is_search || is_admin() ) {470 return $search;471 }472 473 // Get the tax query and replace the leading AND with an OR474 $tax_query = get_tax_sql( $this->get_tax_query( get_query_var( 's' ) ), $wpdb->posts, 'ID' );475 if ( 'and' == substr( trim( strtolower( $tax_query['where'] ) ), 0, 3 ) ) {476 $tax_query['where'] = ' OR ' . substr( trim( $tax_query['where'] ), 3 );477 }478 479 // Mostly taken from query.php480 if ( isset( $query->query_vars['search_terms'] ) ) {481 $search = $searchand = '';482 $n = empty( $query->query_vars['exact'] ) ? '%' : '';483 484 foreach ( (array) $query->query_vars['search_terms'] as $term ) {485 $term = esc_sql( $wpdb->esc_like( $term ) );486 $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";487 $searchand = ' AND ';488 }489 490 // Combine the search and tax queries.491 if ( ! empty( $search ) ) {492 // Add the tax search to the query493 if ( ! empty( $tax_query['where'] ) ) {494 $search .= $tax_query['where'];495 }496 497 $search = " AND ({$search}) ";498 if ( ! is_user_logged_in() ) {499 $search .= " AND ($wpdb->posts.post_password = '') ";500 }501 }502 }503 504 // These are single-use filters, they delete themselves right after they're used.505 add_filter( 'posts_join', array( $this, 'search_posts_join' ), 10, 2 );506 add_filter( 'posts_groupby', array( $this, 'search_posts_groupby' ), 10, 2 );507 508 return $search;509 }510 511 /**512 * Improved Search: posts_join filter513 *514 * This adds the JOIN clause resulting from the taxonomy515 * search. Make sure this filter runs only once per WP_Query request.516 *517 * @global wpdb $wpdb WordPress database abstraction object.518 *519 * @param string $join520 * @param WP_Query $query521 * @return string522 */523 function search_posts_join( $join, &$query ) {524 // Make sure this filter doesn't run again.525 remove_filter( 'posts_join', array( $this, 'search_posts_join' ), 10, 2 );526 527 if ( $query->is_main_query() ) {528 global $wpdb;529 $tax_query = get_tax_sql( $this->get_tax_query( get_query_var( 's' ) ), $wpdb->posts, 'ID' );530 $join .= $tax_query['join'];531 }532 533 return $join;534 }535 536 /**537 * Improved Search: posts_groupby filter538 *539 * Searching with taxonomies may include duplicates when540 * search query matches content and one or more taxonomies.541 * This filter glues all duplicates. Use only once per WP_Query.542 *543 * @global wpdb $wpdb WordPress database abstraction object.544 *545 * @param string $group_by546 * @param WP_Query $query547 * @return string548 */549 function search_posts_groupby( $group_by, &$query ) {550 // Never run this again551 remove_filter( 'posts_groupby', array( $this, 'search_posts_groupby' ), 10, 2 );552 553 global $wpdb;554 $group_by = "$wpdb->posts.ID";555 556 return $group_by;557 }558 559 /**560 * Returns a $tax_query array for an improved search.561 *562 * @param string $search563 * @return array564 */565 function get_tax_query( $search ) {566 $taxonomies = array(567 'producer',568 'speakers', /*'flavor', 'language',*/569 'event'570 );571 572 $terms = get_terms( $taxonomies, array(573 'search' => $search,574 ) );575 $term_ids = wp_list_pluck( $terms, 'term_id' );576 577 $tax_query = array();578 foreach ( $taxonomies as $taxonomy ) {579 $tax_query[] = array(580 'taxonomy' => $taxonomy,581 'terms' => $term_ids,582 );583 }584 $tax_query['relation'] = 'OR';585 586 return $tax_query;587 }588 415 589 416 /** … … 875 702 /** 876 703 * Convert a WPTV Locale to a WP Locale code. 877 * 704 * 878 705 * This isn't all correct, and many of these need to be combined. 879 * 706 * 880 707 * See https://meta.trac.wordpress.org/ticket/1156 881 708 */
Note: See TracChangeset
for help on using the changeset viewer.