Created
April 16, 2024 10:39
-
-
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
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
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'); |
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
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