Skip to content

Instantly share code, notes, and snippets.

@galbaras
Created March 31, 2025 08:04
Show Gist options
  • Save galbaras/5421f3a76ba075e82596706dd3eccb9d to your computer and use it in GitHub Desktop.
Save galbaras/5421f3a76ba075e82596706dd3eccb9d to your computer and use it in GitHub Desktop.
Search the content of every post type for a match to a string
/* Search the content of every post type for a match to a string. Can be useful when a site's been hacked... */
global $wpdb;
$search = 'YOUR_STRING';
$results = $wpdb->get_results(" SELECT ID, post_title, post_type FROM {$wpdb->posts} WHERE post_status = 'publish' and post_content LIKE '%$search%' ");
echo '<h2>Posts Containing <code>' . esc_html( $search ) . '</code></h2>';
if ( ! $results ) {
echo '<p>Not found.</p>';
return;
}
echo '<ul>';
foreach ( $results as $post ) {
$edit_link = get_edit_post_link( $post->ID );
echo '<li>';
if ( $edit_link ) {
echo '<a href="' . esc_url( $edit_link ) . '">';
}
echo esc_html( $post->post_title ) . ' (' . esc_html( $post->post_type ) . ')';
if ( $edit_link ) {
echo '</a>';
}
echo '</li>';
}
echo '</ul>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment