Search WordPress Posts by Keyword with WP_Query and $wpdb LIKE Queries

Get links to all posts that contain a specific phrase

Hi all! Today we continue with SEO questions. Imagine a large website with thousands of pages. We have to find a word, or a part of a word, in the content of the posts, output links to all pages that contain it, and then be able to edit them.

Problem: Search the posts by a word and get links to the posts that contain it.

Solution: A small function with an SQL query, and a few query variants for phpMyAdmin.

The function

Add to functions.php:

<?php
function get_list_links_to_posts_by_phrase( $search_phrase ) {
    global $wpdb;

    $results = $wpdb->get_results( $wpdb->prepare(
        "SELECT ID, post_title FROM {$wpdb->posts}
         WHERE post_content LIKE %s
           AND post_type NOT IN ('revision', 'nav_menu_item')
           AND post_status = 'publish'",
        '%' . $wpdb->esc_like( $search_phrase ) . '%'
    ) );

    if ( ! $results ) {
        return;
    }

    echo '<h3>Links to posts with the phrase "' . esc_html( $search_phrase ) . '":</h3><ol>';
    foreach ( $results as $post ) {
        echo '<li><a href="' . esc_url( get_permalink( $post->ID ) ) . '">' . esc_html( $post->post_title ) . '</a>'
           . ' (<a href="' . esc_url( get_edit_post_link( $post->ID ) ) . '">edit</a>)</li>';
    }
    echo '</ol>';
}

And call it in any convenient place, for example in a template that only you open:

<?php
if ( current_user_can( 'edit_posts' ) ) {
    get_list_links_to_posts_by_phrase( 'price protection' );
}

Note the condition on post_type. Without it the search also finds revisions, and their links open a 404 page. $wpdb->prepare() and esc_like() make the phrase safe in the query even if it contains quotes or %.

Query variants

LIKE finds a part of a word too:

/* Finds 'protect', and 'protection' too */
SELECT ID, post_name FROM wp_posts WHERE post_content LIKE '%protect%' AND post_type NOT IN ('revision');

To find only the whole word, use a regular expression:

/* Finds only the word 'protect' */
SELECT ID, post_name FROM wp_posts
WHERE post_content REGEXP '(^|[[:space:]])protect([[:space:]]|$)' AND post_type NOT IN ('revision');

The same works for a phrase:

/* Finds only the phrase 'high tech' and outputs the IDs and slugs of the posts */
SELECT ID, post_name FROM wp_posts
WHERE post_content REGEXP '(^|[[:space:]])high tech([[:space:]]|$)' AND post_type NOT IN ('revision');

The pattern looks for spaces around the word, so a word next to a punctuation mark or an HTML tag (protect, or <p>protect) is not found. Use [^[:alnum:]] instead of [[:space:]] to cover those cases too.

In some cases the list on the page can be cut off, probably because of the memory or time limit of the web server. Then run the query in phpMyAdmin or the MySQL console: it returns only IDs and slugs and is much lighter.

Sources:

  1. https://stackoverflow.com/questions/9677039/wordpress-how-search-a-post-for-post-content-with-wp-query-class
  2. https://question-it.com/questions/345044/mysql-poisk-tochnogo-slova-iz-stroki