Making WordPress.org

Changeset 13522


Ignore:
Timestamp:
04/15/2024 06:59:14 AM (2 months ago)
Author:
dd32
Message:

Plugin Directory: Admin: Allow searching for plugins by slug.

WP_Query by default doesn't allow searching by post_name, and doesn't allow it to be specified as a searchable field, resulting in this.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/admin/class-customizations.php

    r13512 r13522  
    3535        add_filter( 'query_vars', array( $this, 'query_vars' ) );
    3636        add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
     37        add_filter( 'posts_search', array( $this, 'posts_search' ), 10, 2 );
    3738
    3839        add_action( 'load-edit.php', array( $this, 'bulk_action_plugins' ) );
     
    263264            $query->set( 'meta_query', $meta_query );
    264265        }
     266    }
     267
     268    /**
     269     * Filter searches to search by slug in wp-admin.
     270     *
     271     * WP_Query::parse_search() doesn't allow specifying the post_name field as a searchable field.
     272     *
     273     * @param string    $where    The WHERE clause of the search query.
     274     * @param \WP_Query $wp_query The WP_Query object.
     275     * @return string The WHERE clause of the query.
     276     */
     277    public function posts_search( $where, $wp_query ) {
     278        global $wpdb;
     279
     280        if ( ! $where || ! $wp_query->is_main_query() || ! $wp_query->is_search() ) {
     281            return $where;
     282        }
     283
     284        // WP_Query::parse_search() is protected, so we'll just do a poor job of it here.
     285        $custom_or = $wpdb->prepare(
     286            "( {$wpdb->posts}.post_name LIKE %s )",
     287            '%' . $wpdb->esc_like( $wp_query->get( 's' ) ) . '%'
     288        );
     289
     290        // Merge the custom column search into the existing search SQL.
     291        $where = preg_replace( '#^(\s*AND\s*)(.+)$#i', ' AND ( $2 OR ' . $custom_or . ' )', $where );
     292
     293        return $where;
    265294    }
    266295
Note: See TracChangeset for help on using the changeset viewer.