Created
November 29, 2021 21:09
-
-
Save jmarreros/a12bd3a20aac9dfafaf4b188596aa990 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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