Skip to content

Instantly share code, notes, and snippets.

@lgedeon
Created March 3, 2020 23:32
Show Gist options
  • Save lgedeon/6929c285002a05677fbd8de01392f494 to your computer and use it in GitHub Desktop.
Save lgedeon/6929c285002a05677fbd8de01392f494 to your computer and use it in GitHub Desktop.
Search post_content for keywords across entire database. Ugly but fast.
<?php
add_action(
'after_setup_theme',
function() {
global $wpdb;
foreach ( [ 'twitter.com', 'facebook.com', 'instagram.com', 'youtube.com', 'soundcloud.com' ] as $s ) {
echo "\n$s";
$results = $wpdb->get_results( "SELECT * FROM `wp_3_posts` WHERE `post_content` LIKE '%$s%' ORDER BY `ID` DESC LIMIT 200;" );
//$results = $wpdb->get_results( "SELECT * FROM wp_3_posts LIMIT 10" );
foreach ( $results as $result ) {
preg_match_all( "/.*$s.*/i", $result->post_content, $matches );
foreach ( $matches as $match ) {
echo "\n $result->post_date $result->ID \n";
var_export( $match[0] );
}
}
}
die();
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment