/** * Fetches the search results for the current search query and returns a comma separated string of IDs. * * @since 1.3.3 * * @return string Blank string or comma separated string of search results' IDs */ function bsearch_clause_prepare() { global $wp_query, $wpdb; $search_ids = ''; if ($wp_query->is_search) { $search_query = get_bsearch_query(); $matches = get_bsearch_matches($search_query, 0); // Fetch the search results for the search term stored in $search_query $searches = $matches[0]; // 0 index contains the search results always if ($searches) { $search_ids = implode(',', wp_list_pluck($searches, 'ID')); } } /** * Filters the string of SEARCH IDs returned * * @since 2.0.0 * * @return string $search_ids Blank string or comma separated string of search results' IDs */ return apply_filters('bsearch_clause_prepare', $search_ids); }
/** * Gets the search results. * * @since 1.2 * * @param string $search_query Search term. * @param int|string $limit Maximum number of search results. * @return string Search results */ function get_bsearch_results($search_query = '', $limit = '') { global $wpdb, $bsearch_settings; if (!$limit) { $limit = isset($_GET['limit']) ? intval($_GET['limit']) : $bsearch_settings['limit']; // Read from GET variable. } $bydate = isset($_GET['bydate']) ? intval($_GET['bydate']) : 0; // Order by date or by score? $topscore = 0; $matches = get_bsearch_matches($search_query, $bydate); // Fetch the search results for the search term stored in $search_query. $searches = $matches[0]; // 0 index contains the search results always. if ($searches) { foreach ($searches as $search) { if ($topscore < $search->score) { $topscore = $search->score; } } $numrows = count($searches); } else { $numrows = 1; } $match_range = get_bsearch_range($numrows, $limit); $searches = array_slice($searches, $match_range[0], $match_range[1] - $match_range[0] + 1); // Extract the elements for the page from the complete results array. $output = ''; /* Lets start printing the results */ if ('' != $search_query) { if ($searches) { $output .= get_bsearch_header($search_query, $numrows, $limit); $search_query = preg_quote($search_query, '/'); $keys = explode(' ', str_replace(array("'", "\"", """), "", $search_query)); foreach ($searches as $search) { $score = $search->score; $search = get_post($search->ID); $post_title = get_the_title($search->ID); /* Highlight the search terms in the title */ if ($bsearch_settings['highlight']) { $post_title = preg_replace('/(' . implode('|', $keys) . ')/iu', '<span class="bsearch_highlight">$1</span>', $post_title); } $output .= '<h2><a href="' . get_permalink($search->ID) . '" rel="bookmark">' . $post_title . '</a></h2>'; $output .= '<p>'; $output .= '<span class="bsearch_score">' . get_bsearch_score($search, $score, $topscore) . '</span>'; $before = __('Posted on: ', 'better-search'); $output .= '<span class="bsearch_date">' . get_bsearch_date($search, __('Posted on: ', 'better-search')) . '</span>'; $output .= '</p>'; $output .= '<p>'; if ($bsearch_settings['include_thumb']) { $output .= '<p class="bsearch_thumb">' . get_the_post_thumbnail($search->ID, 'thumbnail') . '</p>'; } $excerpt = get_bsearch_excerpt($search->ID, $bsearch_settings['excerpt_length']); /* Highlight the search terms in the excerpt */ if ($bsearch_settings['highlight']) { $excerpt = preg_replace('/(' . implode('|', $keys) . ')/iu', '<span class="bsearch_highlight">$1</span>', $excerpt); } $output .= '<span class="bsearch_excerpt">' . $excerpt . '</span>'; $output .= '</p>'; } //end of foreach loop $output .= get_bsearch_footer($search_query, $numrows, $limit); } else { $output .= '<p>'; $output .= __('No results.', 'better-search'); $output .= '</p>'; } } else { $output .= '<p>'; $output .= __('Please type in your search terms. Use descriptive words since this search is intelligent.', 'better-search'); $output .= '</p>'; } if ($bsearch_settings['show_credit']) { $output .= '<hr /><p style="text-align:center">'; $output .= __('Powered by ', 'better-search'); $output .= '<a href="https://webberzone.com/plugins/better-search/">Better Search plugin</a></p>'; } /** * Filter formatted string with search results * * @since 1.2 * * @param string $output Formatted results * @param string $search_query Search query * @param int $limit Number of results per page */ return apply_filters('get_bsearch_results', $output, $search_query, $limit); }
/** * Fetches the search results for the current search query and returns a comma separated string of IDs. * * @access public * @return string Blank string or comma separated string of search results' IDs */ function bsearch_clause_prepare() { global $wp_query, $wpdb; $search_ids = ''; if ($wp_query->is_search) { $search_query = trim(bsearch_clean_terms(apply_filters('the_search_query', get_search_query()))); $search_query_transient = substr('bs_' . preg_replace('/[^A-Za-z0-9\\-]/', '', str_replace(" ", "", $search_query)), 0, 40); // Name of the transient limited to 40 chars $matches = get_transient($search_query_transient); if (!$matches) { $matches = get_bsearch_matches($search_query, 0); // Fetch the search results for the search term stored in $search_query set_transient($search_query_transient, $matches, 3600); } $searches = $matches[0]; // 0 index contains the search results always if ($searches) { $search_ids = implode(',', wp_list_pluck($searches, 'ID')); } } return $search_ids; }