Guest User

Untitled

a guest
Nov 16th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Co-Author Plus ES query fix.
  5. *
  6. * With CAP, authors are attached either with the traditional
  7. * author field or via an "author" taxonomy. So we need to check both.
  8. */
  9. add_filter( 'es_posts_request', function( $es_args, $query ) {
  10. if ( ! $query->is_author() ) {
  11. return $es_args;
  12. }
  13.  
  14. // Remove default author filter, as it needs to be in an "OR" block instead.
  15. $removed_author = false;
  16. if ( isset( $es_args['filter']['and'] ) ) {
  17. foreach ( $es_args['filter']['and'] as $index => $es_filter ) {
  18. if ( isset( $es_filter['term']['author_login'] ) ) {
  19. $removed_author = true;
  20. unset( $es_args['filter']['and'][ $index ] );
  21. }
  22. }
  23. }
  24.  
  25. // There isn't a query based on the author_name, so let's not continue.
  26. if ( ! $removed_author || empty( $query->query_vars['author_name'] ) ) {
  27. return $es_args;
  28. }
  29.  
  30. $author_name = sanitize_title( $query->query_vars['author_name'] );
  31. $new_filters = array(
  32. array( 'term' => array( 'author_login' => $author_name ) ),
  33. array( 'term' => array( 'taxonomy.author.name' => $author_name ) ),
  34. );
  35.  
  36. // Merge the two new filters into an "OR" block.
  37. $filter_or = isset( $es_args['filter']['or'] ) ? $es_args['filter']['or'] : array();
  38. $es_args['filter']['or'] = array_merge( $filter_or, $new_filters );
  39.  
  40. return $es_args;
  41. }, 20, 2 );
Add Comment
Please, Sign In to add comment