Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use author ID instead of nicename for author archive queries #46

Closed
wants to merge 8 commits into from
21 changes: 19 additions & 2 deletions adapters/wpcom-vip.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,25 @@

class ES_WP_Query extends ES_WP_Query_Wrapper {
protected function query_es( $es_args ) {
if ( function_exists( 'es_api_search_index' ) ) {
return es_api_search_index( $es_args, 'es-wp-query' );
if ( ! function_exists( 'es_api_search_index' ) ) {
return new WP_Error( 'vip-es-api-missing', 'Missing es_api_search_index method' );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mboynes Previously it was failing silently. I noticed that set_posts() checks for is_wp_error() so this shouldn't introduce any regression.

}

/**
* VIP ES index doesn't have the user_nicename so we fetch the user ID.
* @see https://developer.wordpress.com/docs/elasticsearch/post-doc-schema/
*/
if ( isset( $es_args['post_author.user_nicename'] ) ) {
// slug is mapped to user_nicename by get_data_by() in WP_User
$user_by_slug = get_user_by( 'slug', $es_args['post_author.user_nicename'] );

if ( isset( $user_by_slug->ID ) ) {
$es_args['author_id'] = $user_by_slug->ID;
unset( $es_args['post_author.user_nicename'] );
}
}

return es_api_search_index( $es_args, 'es-wp-query' );
}

protected function set_posts( $q, $es_response ) {
Expand Down Expand Up @@ -74,6 +90,7 @@ public function set_found_posts( $q, $es_response ) {
function vip_es_field_map( $es_map ) {
return wp_parse_args( array(
'post_author' => 'author_id',
'post_author.user_login' => 'author_login',
'post_date' => 'date',
'post_date.year' => 'date_token.year',
'post_date.month' => 'date_token.month',
Expand Down