Making WordPress.org


Ignore:
Timestamp:
11/10/2022 09:02:49 PM (2 years ago)
Author:
coffee2code
Message:

Photo Directory, Search: Limit partial matching of search terms to at least being at the start of a matching term.

Fixes #6569.

File:
1 edited

Legend:

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

    r12203 r12235  
    1616        add_action( 'pre_get_posts',        [ __CLASS__, 'default_search_to_only_photos' ] );
    1717        add_filter( 'posts_join',           [ __CLASS__, 'tag_join_for_search' ], 10, 2 );
     18        add_filter( 'posts_search',         [ __CLASS__, 'limit_partial_matching' ], 9, 2 );
    1819        add_filter( 'posts_search',         [ __CLASS__, 'tag_where_for_search' ], 10, 2 );
    1920        add_filter( 'posts_groupby',        [ __CLASS__, 'tag_groupby_for_search' ], 10, 2 );
     
    9394
    9495    /**
     96     * Limits partial matching of search terms to at least being at the start of
     97     * a matching term.
     98     *
     99     * @param string   $where Search SQL for WHERE clause.
     100     * @param WP_Query $query The current WP_Query object.
     101     * @return string
     102     */
     103    public static function limit_partial_matching( $where, $query ) {
     104        global $wpdb;
     105
     106        if ( ! is_admin() && $query->is_search() && $query->is_main_query() ) {
     107            $where = preg_replace(
     108                "/LIKE '(\{\w+\})([^\{]+)(\{\w+\})'/",
     109                "LIKE '$2$1'",
     110                $where
     111            );
     112        }
     113
     114        return $where;
     115    }
     116
     117    /**
    95118     * Customizes the WHERE clause for frontend searches.
    96119     *
Note: See TracChangeset for help on using the changeset viewer.