• I am using custom posts as blocks on a page. None of those custom posts are accessed directly, but their content is included in another page.

    Is Relevanssi able to search the content of those custom posts and link the results to the public page where the block appears?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Mikko Saari

    (@msaari)

    Yes, but it may need some help from you. You need to add some code that will tell Relevanssi that posts A, B, and C are related to the page X and their contents should be indexed with the page X.

    How that exactly happens depends on how you’re connecting the posts to the pages, but for example you can use the relevanssi_content_to_index filter hook to add extra content to pages before they are indexed. Let’s assume you have the custom post IDs in a custom field block_id:

    add_filter( 'relevanssi_content_to_index', 'rlv_custom_blocks', 10, 2 );
    function rlv_custom_blocks( $content, $post ) {
        $custom_fields = get_post_meta( $post->ID, 'block_id' );
        foreach ( $custom_fields as $field ) {
            $custom_post = get_post( $field );
            $content .= $custom_post->post_content;
        }
        return $content;
    }

    If you want to have the search excerpts use the same post content as building material, you can also apply the same function when doing excerpts:

    add_filter( 'relevanssi_pre_excerpt_content', 'rlv_custom_blocks', 10, 2 );

    Since the parameters are the same, you can just use the same function both for indexing and excerpts.

    Thread Starter eric3d

    (@eric3d)

    Thanks for the quick response. I’ll run some tests.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Using custom posts as building blocks’ is closed to new replies.