Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save quangmai911/73ecd9c445bff752083981ba00dff056 to your computer and use it in GitHub Desktop.
Save quangmai911/73ecd9c445bff752083981ba00dff056 to your computer and use it in GitHub Desktop.
How to add custom table data to Relevanssi's search index
<?php
add_filter( 'relevanssi_content_to_index', 'acfcdt_relevanssi_support', 10, 2 );
add_filter( 'relevanssi_excerpt_content', 'acfcdt_relevanssi_support', 10, 2 );
function acfcdt_relevanssi_support( $content, $post ) {
/**
* Approach A (recommended): Using SQL to minimise database queries during Relevanssi's indexing process.
*/
global $wpdb;
$data = $wpdb->get_results( "SELECT `book_name`,`book_year` FROM `wp_bookmeta` WHERE post_id = $post->ID", ARRAY_A );
if ( ! empty( $data[0] ) ) {
$values = array_values( $data[0] );
$content .= join( ' ', $values );
}
/**
* Approach B: Using ACF's get_field() function on individual fields
*/
//$content .= get_field( 'book_name', $post->ID );
//$content .= get_field( 'book_year', $post->ID );
return $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment