Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
}
@danielkg

This comment has been minimized.

Copy link

commented Jul 19, 2018

You're missing the closing parenthesis in the where statement at the end:

" 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'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.