Help building a custom search
-
Running into the same issue as others in this forum but can’t find a solution. I can’t seem to get the co-authors returned in a search with custom taxonomy query.
$tax_query_args = array(
‘relation’ => ‘OR’,
array(
‘taxonomy’ => ‘author’,
‘terms’ => 257, // look up this id from an author id
‘field’ => ‘id’,
‘include_children’ => false,
‘operator’ => ‘IN’
)
);
$query->set(‘tax_query’, $tax_query_args);My generated sql is close to the sql used on the author lookup page.
// not working – generated from custom taxonomy search
SELECT SQL_CALC_FOUND_ROWS afwp_posts.ID FROM afwp_posts LEFT JOIN afwp_term_relationships ON (afwp_posts.ID = afwp_term_relationships.object_id) LEFT JOIN afwp_term_taxonomy ON ( afwp_term_relationships.term_taxonomy_id = afwp_term_taxonomy.term_taxonomy_id ) WHERE 1=1 AND afwp_posts.post_author IN (32) AND afwp_posts.post_type = ‘post’ AND (afwp_posts.post_status = ‘publish’) GROUP BY afwp_posts.ID HAVING MAX( IF( afwp_term_taxonomy.taxonomy = ‘author’, IF( afwp_term_taxonomy.term_id = ‘257’,2,1 ),0 ) ) <> 1 ORDER BY afwp_posts.post_date DESC LIMIT 0, 10// working from author page
/*SELECT SQL_CALC_FOUND_ROWS afwp_posts.ID FROM afwp_posts LEFT JOIN afwp_term_relationships ON (afwp_posts.ID = afwp_term_relationships.object_id) LEFT JOIN afwp_term_taxonomy ON ( afwp_term_relationships.term_taxonomy_id = afwp_term_taxonomy.term_taxonomy_id ) WHERE 1=1 AND ((afwp_posts.post_author = 32 OR (afwp_term_taxonomy.taxonomy = ‘author’ AND afwp_term_taxonomy.term_id = ‘257’))) AND afwp_posts.post_type = ‘post’ AND (afwp_posts.post_status = ‘publish’) GROUP BY afwp_posts.ID HAVING MAX( IF( afwp_term_taxonomy.taxonomy = ‘author’, IF( afwp_term_taxonomy.term_id = ‘257’,2,1 ),0 ) ) <> 1 ORDER BY afwp_posts.post_date DESC LIMIT 0, 10The difference seems to be that I am not able to generate this bit of sql from the wp_query object with the custom taxonomy code.
((afwp_posts.post_author = 32 OR (afwp_term_taxonomy.taxonomy = ‘author’ AND afwp_term_taxonomy.term_id = ‘257’)))
but rather this is generated, only taking into account the wp author id.
afwp_posts.post_author IN (32)I have been injecting this via the pre_get_posts action.
add_action(‘pre_get_posts’,’search_filter’);Am I missing something crucial to add a co-author search to the standard search? When someone searches for a post by a co-author, none are returned since WP is only looking at the author_id. I tried to add the co-author taxonomy ‘author’ to the search query to include posts that match a co-author, but none are returned.
- The topic ‘Help building a custom search’ is closed to new replies.