My site requires that people can search for terms that are included in tags and categories (towns and counties).

I'm having some resource issues using the 'Search Everything' plugin, it's causing the mysql database to lock and giving me massive slowdown. I don't blame the plugin, I blame my shared hosting.

BUT, I was wondering if I could avoid using a plugin at all by simply appending the post tags and category names to the_content within the database (instead of using a filter which I believe would only include the terms as they are rendered on the page) so that the in-built WordPress search could then be satisfactory and would find these terms.

I've read lots of different codex pages and tutorials and not found what I need, but I'm hoping someone could have a look at the following code and give their thoughts on whether it might work (I do not have a test server available at the moment and am hesitant to run it on a live site!) (Note this example includes tags only so far)

function tags_in_content($post_id, $post) {

$content = get_the_content();
$tags = get_tags(); 


add_action ( 'publish_post', 'tags_in_content' );

It can't be that simple can it?!

Many thanks for any help anyone can give me!

1 Answer 1


I would say the best way to do this is using the save_post action, which is run every time a post is modified in edit screen. It will require a fair bit of fiddling when writing and reading post_content to/from the database. In the example below I add a list of post tags to post_content. A tricky but is the remove and readd the filter inside of its own callback, otherwise you'll end up with a infinite loop.

add_action( 'save_post', 'my_save_post' );
function my_save_post( $post_id ) {
    $divider = "<!-- DIVIDER -->"; // We'll use this as a divider of actual post_content and our tag list

    //verify post is not a revision
    if ( !wp_is_post_revision( $post_id ) ) {

        // Remove filter to avoid infinite loop
        add_action( 'save_post', 'my_save_post' );

        $post = get_post($post_id);
        $content = $post->post_content;

        // Strip away old tags
        $editor_content = array_shift(explode($divider, $content));

        // Add new tag list to end of post_content
        $editor_content .= "\n" . $divider . "\n" .implode(", ", wp_get_post_terms($post_id, 'post_tag'));

        // Update post object and and database
        $post->post_content = $editor_content;

        // Add filter again
        add_action( 'save_post', 'my_save_post' );

You would probably also need to add a filter to remove the tag list form post_content before it's being sent to the editor textarea. I think the_editor_content will do quite a good job in this case. What you want to do is basically the same thing as the first half of the above function, i.e. stripping away everything after the divider but with out appending a new tag list (we don't want that showing up in the editor, right?). This means you can break that out into its own function and use that in both cases, but I'll leave that to you.

  • Brilliant, thank you Simon. Admittedly I'm not experienced this in-depth in WP code but that does all make sense (thanks to your helpful comments). I will test it out asap.
    – Jo_pinkish
    Commented Jan 30, 2013 at 12:20
  • No problem, quite an intriguing question! As I wrote in my answer, this method will require a bit of work, so if you are doing it just to speed up searching there might be easier options. Here's an idea: use the save_post action to list all tags, categories, meta fields etc. that you wish to leverage in searches and treat the result as a kind of "keyword list". Save this list using update_post_meta and make the default search function look into that specific meta values during searches (there should be a action/filter-hook you can use for that).
    – Simon
    Commented Jan 30, 2013 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.