Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.