Last active
September 9, 2020 20:55
-
-
Save WPprodigy/a6e42dcb94bf4fb93201cf9065d79a7f to your computer and use it in GitHub Desktop.
Co-Author Plus ES query fix
This file contains 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 | |
/** | |
* Co-Author Plus ES query fix. | |
* | |
* With CAP, authors are attached either with the traditional | |
* author field or via an "author" taxonomy. So we need to check both. | |
*/ | |
add_filter( 'es_posts_request', function( $es_args, $query ) { | |
if ( ! $query->is_author() ) { | |
return $es_args; | |
} | |
// Remove default author filter, as it needs to be in an "OR" block instead. | |
$removed_author = false; | |
if ( isset( $es_args['filter']['and'] ) ) { | |
foreach ( $es_args['filter']['and'] as $index => $es_filter ) { | |
if ( isset( $es_filter['term']['author_login'] ) ) { | |
$removed_author = true; | |
unset( $es_args['filter']['and'][ $index ] ); | |
} | |
} | |
} | |
// There isn't a query based on the author_name, so let's not continue. | |
if ( ! $removed_author || empty( $query->query_vars['author_name'] ) ) { | |
return $es_args; | |
} | |
$author_name = sanitize_title( $query->query_vars['author_name'] ); | |
$new_filters = array( | |
array( 'term' => array( 'author_login' => $author_name ) ), | |
array( 'term' => array( 'taxonomy.author.name.raw_lc' => $author_name ) ), | |
); | |
// Merge the two new filters into an "OR" block. | |
$filter_or = isset( $es_args['filter']['or'] ) ? $es_args['filter']['or'] : array(); | |
$es_args['filter']['or'] = array_merge( $filter_or, $new_filters ); | |
return $es_args; | |
}, 20, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not fully tested, but the elasticpress version would look something like this: