Plugin Directory

Changeset 1920390


Ignore:
Timestamp:
08/06/2018 12:07:57 PM (8 years ago)
Author:
8bitsinarow
Message:

Improved search results to move exact matches on top

Location:
basepress/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • basepress/trunk/basepress.php

    r1910839 r1920390  
    55 * Plug URI: http://www.8bitsinarow.com
    66 * Description: The perfect Knowledge Base plugin for WordPress
    7  * Version: 1.8.5
     7 * Version: 1.8.6
    88 * Author: 8Bits in a row
    99 * Author URI: http://www.8bitsinarow.com
     
    7373             * @var string
    7474             */
    75             public  $ver = '1.8.5' ;
     75            public  $ver = '1.8.6' ;
    7676            /**
    7777             * Database version
  • basepress/trunk/includes/class-basepress-cpt.php

    r1833522 r1920390  
    554554                if ( 'edit' == $action && 'knowledgebase' == $post_type ) {
    555555                    $missing_options = array();
    556                     if ( empty(get_the_terms( $post->ID, 'knowledgebase_cat' )[0]) ) {
     556                    $post_terms = get_the_terms( $post->ID, 'knowledgebase_cat' )[0];
     557                    $post_meta = get_post_meta( $post->ID, 'basepress_template_name', true );
     558                    if ( empty($post_terms) ) {
    557559                        $missing_options[] = __( 'Section', 'basepress' );
    558560                    }
    559                     if ( empty(get_post_meta( $post->ID, 'basepress_template_name', true )) ) {
     561                    if ( empty($post_meta) ) {
    560562                        $missing_options[] = __( 'Template', 'basepress' );
    561563                    }
  • basepress/trunk/public/class-basepress-search.php

    r1910839 r1920390  
    334334
    335335                $terms = str_replace( '+', ' ', $terms );
    336                 $query_pieces = $this->query_pieces( $product, $terms );
     336                $query_pieces = $this->query_pieces( $product, $terms, $pieces );
    337337
    338338                if ( $query_pieces ) {
     
    368368         *
    369369         * @since 1.2.1
    370          * @updated 1.4.0, 1.7.5, 1.7.11, 1.8.1
     370         * @updated 1.4.0, 1.7.5, 1.7.11, 1.8.1, 1.8.6
    371371         *
    372372         * @param $product
     
    374374         * @return array|null|object
    375375         */
    376         private function query_pieces( $product, $terms ) {
     376        private function query_pieces( $product, $terms, $pieces = array() ) {
     377
    377378            global $wpdb;
    378379
     
    384385                $terms = strtolower( $terms );
    385386            }
     387
     388            //Set exact matches terms before filtering the query
     389            $exact_match_terms = $terms;
     390
    386391            //Filter out unwanted terms
    387392            $terms = $this->filter_terms( $terms );
     
    421426
    422427                $_sections_ids = $wpdb->get_results( $sections_query, ARRAY_A );
    423                
     428
    424429                $sections_ids = array();
    425430
     
    431436
    432437                //Prepare matches portion of the query
     438                $order_index = 1;
    433439                $orderby = '';
    434                 foreach ( $sql_terms as $sql_term ) {
    435                     $orderby .= " + CASE WHEN $wpdb->posts.post_content LIKE '$sql_term%' THEN 1 ELSE 0 END";
    436                     $orderby .= " + CASE WHEN $wpdb->posts.post_title LIKE '$sql_term%' THEN 1 ELSE 0 END";
    437                 }
    438                 $orderby = ltrim( $orderby, ' + ' );
    439                 $orderby .= " DESC";
    440 
     440
     441                //Exact matches
     442                $orderby .= " WHEN $wpdb->posts.post_title REGEXP '[[:<:]]$exact_match_terms' THEN $order_index";
     443                $order_index++;
     444                $orderby .= " WHEN $wpdb->posts.post_content REGEXP '[[:<:]]$exact_match_terms' THEN $order_index";
     445                $order_index++;
     446
     447                //AND logic
     448                foreach( array( 'title', 'content' ) as $field ){
     449                    $orderby .= ' WHEN';
     450                    foreach( $sql_terms as $index => $sql_term ){
     451                        $sql_term = $sql_term;
     452                        $orderby .= $index != 0 ? ' AND' : '';
     453                        $orderby .= " $wpdb->posts.post_$field REGEXP '[[:<:]]$sql_term'";
     454                    }
     455                    $orderby .= " THEN $order_index";
     456                    $order_index++;
     457                }
     458
     459                //OR logic
     460                foreach( array( 'title', 'content' ) as $field ){
     461                    $orderby .= ' WHEN';
     462                    foreach( $sql_terms as $index => $sql_term ){
     463                        $sql_term = $sql_term;
     464                        $orderby .= $index != 0 ? ' OR' : '';
     465                        $orderby .= " $wpdb->posts.post_$field REGEXP '[[:<:]]$sql_term'";
     466                    }
     467                    $orderby .= " THEN $order_index";
     468                    $order_index++;
     469                }
     470
     471                //Close ORDER BY clause
     472                $orderby = '(CASE ' . ltrim( $orderby, ' + ' ) . " ELSE $order_index END)";
     473
     474                //Join Clause
    441475                $pieces['join'] = " LEFT JOIN $wpdb->term_relationships AS t ON ($wpdb->posts.ID = t.object_id)";
    442476
     477                //Where Clause
    443478                $pieces['where'] = " AND t.term_taxonomy_id IN ($sections_ids)";
    444479                $pieces['where'] .= " AND ( LOWER($wpdb->posts.post_content) REGEXP '$multi_terms' OR LOWER($wpdb->posts.post_title) REGEXP '$multi_terms' )";
     
    449484
    450485                /**
    451                     * Filter to alter the query pieces for the search query
    452                     */
    453                     $pieces = apply_filters( 'basepress_search_query_pieces', $pieces );
     486                 * Filter to alter the query pieces for the search query
     487                 */
     488                $pieces = apply_filters( 'basepress_search_query_pieces', $pieces );
    454489
    455490                $pieces['orderby'] .= ", $wpdb->posts.post_date DESC";
  • basepress/trunk/readme.txt

    r1910839 r1920390  
    55Requires at least: 4.5
    66Tested up to: 4.9
    7 Stable tag: 1.8.5
     7Stable tag: 1.8.6
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    138138== Changelog ==
    139139
     140= 1.8.6 =
     141* Improved search results to move exact matches on top
     142
    140143= 1.8.5 =
    141144* Removed page builders' shortcodes from search snippets
Note: See TracChangeset for help on using the changeset viewer.