Skip to content

Instantly share code, notes, and snippets.

@hearvox
Last active February 8, 2018 17:35
Show Gist options
  • Save hearvox/4130c6412b4e5f5c561a457b17d08d39 to your computer and use it in GitHub Desktop.
Save hearvox/4130c6412b4e5f5c561a457b17d08d39 to your computer and use it in GitHub Desktop.
Find WordPress posts that don't contain a specific search term.
<?php
/* Get IDs of posts with content that doesn't contain a search term. */
$keyword = 'search term here';
$max_posts = 400; // More than the total number of posts.
$query_ids = new WP_Query( array( 'post_type' => 'post', 'fields' => 'ids', 'posts_per_page' => $max_posts ) );
$query_search = new WP_Query( array( 'post_type' => 'post', 'fields' => 'ids', 'posts_per_page' => $max_posts, 's' => $keyword ) );
$ids_not_found = array_diff( $query_ids->posts, $query_search->posts );
sort( $ids_not_found );
print_r( $ids_not_found );
/*
// List found posts with edit and view links.
echo '<ol>';
foreach ( $ids_not_found as $post_id ) {
echo "<li>{$post_id} <a href='/?p={$post_id}'>view</a> | <a href='/wp-admin/post.php?post={$post_id}&action=edit'>edit</a><br>";
}
echo '</ol>';
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment