Skip to content

Instantly share code, notes, and snippets.

@jmarreros
Created November 29, 2021 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmarreros/a12bd3a20aac9dfafaf4b188596aa990 to your computer and use it in GitHub Desktop.
Save jmarreros/a12bd3a20aac9dfafaf4b188596aa990 to your computer and use it in GitHub Desktop.
<?php
function dcms_count_text_search( $query_object ) {
if( $query_object->is_search() ) {
global $wpdb;
$table = $wpdb->prefix . 'text_search'; //Nombre de la tabla
$col_search = 'search'; //unique
$col_count = 'count';
$search_text = $query_object->query['s'];
// Buscamos la cantidad de repeticiones si el texto existe
$sql = $wpdb->prepare("SELECT `$col_count` FROM `$table` WHERE `$col_search` = '%s'", $search_text);
$count = $wpdb->get_var($sql) ?? 0;
// Insertamos o actualizamos
$wpdb->replace( $table, [$col_search => $search_text, $col_count => $count + 1], ['%s', '%d'] );
}
}
add_action( 'parse_query', 'dcms_count_text_search' );
// # Base table:
// CREATE TABLE wp_text_search (
// search VARCHAR(200) NOT NULL UNIQUE,
// count INT DEFAULT 0
// );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment