Posted on

bbPress: Indexing topics and replies

Relevanssi does work with BBPress. BBPress forums, topics and replies are regular WordPress posts. In order to get Relevanssi working with BBPress, you need to make Relevanssi index post type topic (forget forum, and I think it’s better if you skip reply as well).

Since topic is non-public post type, you also need to uncheck the Respect exclude_from_search option.

Now, these settings won’t notice replies. You could choose to index replies, but then you can get results where you have many replies from the same topic. I suggest instead skipping the replies in the indexing, but adding this code to your site:

add_filter( 'relevanssi_content_to_index', 'rlv_add_replies', 10, 2 );
add_filter( 'relevanssi_excerpt_content', 'rlv_add_replies', 10, 2 );
function rlv_add_replies( $content, $post ) {
  if ( 'topic' === $post>post_type ) {
    $replies = get_posts(
      array(
        'post_type'      => 'reply',
        'post_parent'    => $post->ID,
        'posts_per_page' => -1,
        'orderby'        => 'date',
        'order'          => 'ASC'
      )
    );
    if ( is_array( $replies ) ) {
      foreach ( $replies as $reply ) {
        $content .= ' ' . $reply->post_content . ' ';
      }
    }
  }
  return $content;
}

add_filter( 'wp_insert_post', 'rlv_index_replies' );
function rlv_index_replies( $post_id ) {
  $post = get_post( $post_id );
  if ( 'reply' === $post->post_type ) {
    relevanssi_index_doc( $post->post_parent, $remove_first = true, $custom_fields = false, $bypassglobalpost = true );
	}
}

This will add the contents of the replies to the topic posts. That way the whole conversation is indexed in one go.

The second function is also important. It makes sure that the topics are reindexed when new replies are added (this doesn’t trigger a reindex of the topic when a reply is removed; I’ll leave that as an exercise for the reader).

6 comments bbPress: Indexing topics and replies

  1. I like the philosophy here. After adding the code, I was able to confirm that the reply content is being properly concatenated to the topic content when the index is being built. But afterward, when I try to do an actual search for terms in one of the replies, the appropriate topics are not returned in the search results. When I search for specific text in a reply, I get no results.

    Due to the way that the index is stored in the DB, this is a hard thing for me to troubleshoot. Any ideas?

    1. One way to be sure that Relevanssi is indexing the replies correctly is to add a reply with a word that only appears in that one reply. Then go see wp_relevanssi database table and see if that word appears. It should appear, with the “doc” column matching the topic post ID.

      1. Hmm, that seemed to have worked. I’m thinking it was maybe a problem with the data from my imported forums… Oh well, I think we’re good.

        My only suggestion would be to modify rlv_add_replies() order the replies when they are pulled so that they match the order they appear on the topic page:

        $replies = get_posts(array(‘post_type’ => ‘reply’, ‘post_parent’ => $post->ID, ‘posts_per_page’ => -1, ‘orderby’ => ‘date’, ‘order’ => ‘ASC’));

        This way, the generated excerpt will make a bit more sense.

        Thanks!

  2. Hello, any suggestions on how I might debug? I’m at a loss.
    Follow the advice above, if I search the post ID in the debug panel it works, yet no BBpress results can be found for the frontend search.

    Thanks in advance.

Leave a Reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

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