Posted on

Indexing additional content

Sometimes you may want to index additional content to your posts. Perhaps dynamic content generated by some PHP script, perhaps data from another, external database. It is quite easy, as there’s a simple filter you can use.

When Relevanssi indexes a post, it calls relevanssi_content_to_index to see if there’s additional content to index. This filter has two parameters: the content and the post.

The basic usage looks like this, here’s a sample case where additional content is fetched from a database table based on post ID:

add_filter('relevanssi_content_to_index', 'add_extra_content', 10, 2);
function add_extra_content($content, $post) {
    global $wpdb;
    $content = $wpdb->get_var("SELECT data FROM extra_table WHERE post_id = $post->ID");
    return $content;
}

$content is empty, so if you have only one function attached to the filter, you don’t have to worry about it.

It’s this simple. Do note that whenever the content changes, Relevanssi index is not updated. Relevanssi index is only updated when the post is saved.

If you want the additional content to appear in excerpts as well, you can use the same function, but in this case you must retain the original contents of $content:

add_filter('relevanssi_content_to_index', 'add_extra_content', 10, 2);
add_filter('relevanssi_excerpt_content', 'add_extra_content', 10, 2);
function add_extra_content($content, $post) {
    global $wpdb;
    $content .= $wpdb->get_var("SELECT data FROM extra_table WHERE post_id = $post->ID");
    return $content;
}

This adds the content both to index and to excerpts.

Once you’ve created the functions and added them to your site (the functions.php file of your theme is usually the best place), it works automatically and no changes to Relevanssi settings are necessary.

8 comments Indexing additional content

  1. I need Thumbnails of Images in the search result. Why it don’t go?

    My Code in Wp search.php :

    post_type == ‘attachment’): ?>

    <a class="fancyimg" rel="fancygallery" href="ID); ?>”>ID, ‘thumbnail’, 0); ?>
    <a class="fancyimg fancycaption" rel="fancygallery" href="ID); ?>”>

    But i get no preview ๐Ÿ™

    Must i edit the functions.php or relevanssi plugin code for pictures in the search result?

    Please give me introductions how can i fix it….Thanks

  2. I need Thumbnails of Images in the search result. Why it don’t go?

    My Code in Wp search.php :

    post_type == 'attachment'): ?>

    <a class="fancyimg" rel="fancygallery" href="ID); ?>">ID, 'thumbnail', 0); ?>

    <a class="fancyimg fancycaption" rel="fancygallery"
    href="ID);
    ?>">


    But i get no preview ๐Ÿ™

    Must i edit the functions.php or relevanssi plugin code for pictures in the search result?

    Please give me introductions how can i fix it….Thanks

  3. I need Thumbnails of Images in the search result. Why it don’t go?

    My Code in Wp search.php :

    post_type == 'attachment'): ?>

    <a class="fancyimg" rel="fancygallery" href="ID); ?>">ID, 'thumbnail', 0); ?>

    <a class="fancyimg fancycaption" rel="fancygallery"
    href="ID);
    ?>">

    But i get no preview ๐Ÿ™

    Must i edit the functions.php or relevanssi plugin code for pictures in the search result?

    Please give me introductions how can i fix it….Thanks

    1. Please put the code to a pastebin, so it doesn’t get mangled. All you need to do to fix the problem is to adjust the search results template, so you’re on the right track.

  4. Is this possible with users too? (on premium version)

    I’m trying to index a field from the database using the function below, but no post-type ‘user’s use it. Is there an equivalent filter for users?

    //add buddypress ‘about me’ database field to relevanssi index
    add_filter(‘relevanssi_content_to_index’, ‘add_extra_content’, 10, 2);
    add_filter(‘relevanssi_excerpt_content’, ‘add_extra_content’, 10, 2);
    function add_extra_content($content, $post) {
    global $wpdb;
    $aboutme = $wpdb->get_var(“SELECT value FROM wp_bp_xprofile_data WHERE user_id = $post->ID AND field_id = 15”); // 15 is our about-me field
    // echo get_post_type( $post->ID);
    $content .= $aboutme;
    return $content;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *