Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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' => $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 );
Add Comment
Please, Sign In to add comment