Skip to content

Instantly share code, notes, and snippets.

@safwanafridi
Created April 16, 2024 10:39
Show Gist options
  • Save safwanafridi/fccf83c61c32526ebcdf4653077152e9 to your computer and use it in GitHub Desktop.
Save safwanafridi/fccf83c61c32526ebcdf4653077152e9 to your computer and use it in GitHub Desktop.
How to display Most Searched Keywords below Search Bar in WordPress Website - Free method to add this functionality without using the Paid Plugin
function display_top_search_keywords() {
global $wpdb;
// Get the table name with the correct prefix
$table_name = $wpdb->prefix . 'relevanssi_log';
// Query to fetch the most searched keywords and their counts
$query = "SELECT query, COUNT(*) AS search_count
FROM $table_name
GROUP BY query
ORDER BY search_count DESC
LIMIT 4";
// Fetch the results
$results = $wpdb->get_results($query);
// Output the results
ob_start();
if ($results) {
echo '<ul>';
foreach ($results as $result) {
echo '<li><a href="' . get_search_link($result->query) . '">' . $result->query . '</a></li>';
}
echo '</ul>';
}
return ob_get_clean();
}
add_shortcode('top_search_keywords', 'display_top_search_keywords');
Watch this Video (Language: Urdu) = https://www.linkedin.com/posts/safwanafridi_wordpress-wordpresscommunity-wordpresstips-activity-7185940917751586816-QHoC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment