Skip to content

Instantly share code, notes, and snippets.

@mgibbs189
Last active December 29, 2020 14:07
Show Gist options
  • Save mgibbs189/01350ecaa74f3c7f967b2ea8e32c4ed6 to your computer and use it in GitHub Desktop.
Save mgibbs189/01350ecaa74f3c7f967b2ea8e32c4ed6 to your computer and use it in GitHub Desktop.
FacetWP - pull in comment data
<?php
add_filter( 'facetwp_indexer_row_data', function( $rows, $params ) {
/* index the comment ratings */
if ( 'comment_rating' == $params['facet']['source'] ) {
$post_id = (int) $params['defaults']['post_id'];
$comments = get_comments( [ 'post_id' => $post_id ] );
foreach ( $comments as $comment ) {
$new_row = $params['defaults'];
$new_row['facet_value'] = $comment->comment_content;
$new_row['facet_display_value'] = $comment->comment_content;
$rows[] = $new_row;
}
}
/* index the comment_authors */
elseif ( 'comment_author' == $params['facet']['source'] ) {
$post_id = (int) $params['defaults']['post_id'];
$comments = get_comments( [ 'post_id' => $post_id ] );
foreach ( $comments as $comment ) {
$new_row = $params['defaults'];
$new_row['facet_value'] = $comment->comment_author;
$new_row['facet_display_value'] = $comment->comment_author;
$rows[] = $new_row;
}
}
return $rows;
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment