WordPress.org

Making WordPress.org


Ignore:
Timestamp:
09/17/2015 06:46:55 AM (5 years ago)
Author:
dd32
Message:

Themes Directory: Allow searches to match on theme slug in addition to tags, content, and title.
Fixes #1021

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/theme-directory.php

    r1845 r1884  
    687687}
    688688
     689/**
     690 * Extends repopackage searches to include theme slugs.
     691 *
     692 * @param string   $search   Search SQL for WHERE clause.
     693 * @param WP_Query $wp_query The current WP_Query object.
     694 * @return string
     695 */
     696function wporg_themes_search_slug( $search, $wp_query ) {
     697    if ( empty( $search ) || 'repopackage' !== $wp_query->query_vars['post_type'] || ! $wp_query->is_search() ) {
     698        return $search;
     699    }
     700
     701    global $wpdb;
     702    $n = empty( $wp_query->query_vars['exact'] ) ? '%' : '';
     703    $search = $searchand = '';
     704
     705    foreach ( $wp_query->query_vars['search_terms'] as $term ) {
     706        $like    = $n . $wpdb->esc_like( $term ) . $n;
     707        $search .= $wpdb->prepare( "{$searchand}(($wpdb->posts.post_title LIKE %s) OR ($wpdb->posts.post_name LIKE %s) OR ($wpdb->posts.post_content LIKE %s))", $like, $like, $like );
     708        $searchand = ' AND ';
     709    }
     710
     711    if ( ! empty( $search ) ) {
     712        $search = " AND ({$search}) ";
     713        if ( ! is_user_logged_in() ) {
     714            $search .= " AND ($wpdb->posts.post_password = '') ";
     715        }
     716    }
     717
     718    return $search;
     719}
     720add_filter( 'posts_search', 'wporg_themes_search_slug', 10, 2 );
    689721
    690722/**
Note: See TracChangeset for help on using the changeset viewer.