• Resolved seanpaulfx

    (@seanpaulfx)


    Hello
    I am trying to add custom tables to Relevanssi but it is not working.
    I am using this filter

    add_filter( 'relevanssi_content_to_index', 'rlv_add_extra_content', 10, 2 );
    function rlv_add_extra_content( $content, $post ) {
    	global $wpdb;
    	$data = $wpdb->get_results( "SELECT title, title2, title3 FROM wp_test_title WHERE ID = $post->ID", ARRAY_A );
    	if ( ! empty( $data[0] ) ) {
    		$values  = array_values( $data[0] );
    		$content .= join( ' ', $values );
    	}
    	return $content;
    }

    I have tested the code, it is working properly, i have tried indexing many times but there doesn’t seem to be any change.

    Thank you

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

    (@msaari)

    The code seems to be correct. You can also check the posts with the Relevanssi debugger to see if the new content is being indexed correctly. Is it?

    I can think of two reasons why this isn’t working:

    – The content that’s coming from the table is not indexed for some reason; maybe the words are too short, or stopwords? Checking the Relevanssi debugger will confirm if the indexing of the content works.

    – Your search is not actually using Relevanssi. Can you find this content if you use the Relevanssi admin search at Dashboard > Admin search?

    Thread Starter seanpaulfx

    (@seanpaulfx)

    I found the problem

    add_filter( ‘relevanssi_index_content’, ‘__return_false’ );

    Since I used this filter before, an error occurred.

    I don’t want to index the post content, is there any way I can index the titles in a custom table.

    Plugin Author Mikko Saari

    (@msaari)

    Yes, you’re blocking content indexing and adding stuff to post content. The easiest solution here is to remove the relevanssi_index_content filter and then do the content adding filter this way:

    add_filter( 'relevanssi_post_content', 'rlv_add_extra_content', 10, 2 );
    function rlv_add_extra_content( $content, $post ) {
    	global $wpdb;
    
    	$content = '';
    	$data = $wpdb->get_results( "SELECT title, title2, title3 FROM wp_test_title WHERE ID = $post->ID", ARRAY_A );
    	if ( ! empty( $data[0] ) ) {
    		$values  = array_values( $data[0] );
    		$content .= join( ' ', $values );
    	}
    	return $content;
    }

    Ie. the same as before, except this uses relevanssi_post_content, so it filters the existing post content, and then it blanks it out before adding the new content.

    Thread Starter seanpaulfx

    (@seanpaulfx)

    Thank you!, it worked

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom Table’ is closed to new replies.