SELECT
SQL_CALC_FOUND_ROWS DISTINCT wp_posts.ID,
bar_terms.term_id
FROM
wp_posts
INNER JOIN wp_postmeta ON (
wp_posts.ID = wp_postmeta.post_id
)
LEFT JOIN wp_postmeta as bar_post_meta ON wp_posts.ID = bar_post_meta.post_id
LEFT JOIN wp_term_relationships as bar_term_relationships ON wp_posts.ID = bar_term_relationships.object_id
LEFT JOIN wp_term_taxonomy as bar_term_taxonomy ON bar_term_relationships.term_taxonomy_id = bar_term_taxonomy.term_taxonomy_id
LEFT JOIN wp_terms as bar_terms ON bar_term_taxonomy.term_id = bar_terms.term_id
WHERE
1 = 1
AND (
1 = 1
AND (
(
(wp_posts.post_title LIKE '%a%')
OR (wp_posts.post_excerpt LIKE '%a%')
OR (wp_posts.post_content LIKE '%a%')
)
)
OR (
bar_post_meta.meta_key = '_sku'
AND bar_post_meta.meta_value LIKE '%a%'
)
OR (
bar_term_taxonomy.taxonomy = 'product_cat'
AND bar_terms.name LIKE '%a%'
)
)
AND (
(
wp_postmeta.meta_key = '_stock_status'
AND wp_postmeta.meta_value NOT IN ('outofstock')
)
)
AND wp_posts.post_type = 'product'
AND (
(wp_posts.post_status = 'publish')
)
GROUP BY
wp_posts.ID,
bar_terms.term_id
ORDER BY
FIELD(bar_terms.term_id, 30) DESC
LIMIT
0, 100