Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielkg/e811f02ae12e31c7655e45ebcdda8516 to your computer and use it in GitHub Desktop.
Save danielkg/e811f02ae12e31c7655e45ebcdda8516 to your computer and use it in GitHub Desktop.
Include posts from Co-Authors Plus Guest Authors in Wordpress' search results
<?php defined('ABSPATH') or die();
/**
* Include posts from guest authors in the search results.
* @author jakebellacera
*/
add_filter('posts_clauses', 'search_posts_clauses', 10, 2);
function search_posts_clauses($clauses, $query) {
if (!$query->is_main_query && !$query->is_search && !$query->is_admin)
return $clauses;
global $wpdb;
$clauses['join'] .= <<<SQL
INNER JOIN {$wpdb->term_relationships} ON {$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id
INNER JOIN {$wpdb->term_taxonomy} USING (term_taxonomy_id)
INNER JOIN {$wpdb->terms} USING (term_id)
SQL;
$clauses['where'] .= $wpdb->prepare(
" OR ({$wpdb->term_taxonomy}.taxonomy = 'author' AND {$wpdb->term_taxonomy}.description LIKE '%%%s%%' AND {$wpdb->posts}.post_type = 'post')",
sanitize_text_field($query->query['s'])
);
return $clauses;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment