Skip to content

Instantly share code, notes, and snippets.

@mishterk
Created March 11, 2019 04:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mishterk/736b6f01f79edc4f06295e22213f70db to your computer and use it in GitHub Desktop.
Save mishterk/736b6f01f79edc4f06295e22213f70db 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