Created
March 11, 2019 04:10
-
-
Save mishterk/736b6f01f79edc4f06295e22213f70db to your computer and use it in GitHub Desktop.
How to add custom table data to Relevanssi's search index
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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