Created
January 19, 2018 14:24
-
-
Save CatEntangler/284b731fdcb7b3b8e4d9054ed54394d1 to your computer and use it in GitHub Desktop.
WordPress: Return category result count from product post search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This query looks into post fields and returns an ordered list of results based on the attached categories to the posts searched. | |
This can be used to help weight results or to potentially show links to categories instead of the posts themselves. | |
Can be modified to add additional filters. Still not sure how to tie this into pre-get-posts but working on it. | |
+------------+---------+--------------+ | |
| name | term_id | result_count | | |
+------------+---------+--------------+ | |
| Category A | 15 | 12 | | |
| Category C | 17 | 12 | | |
| Category B | 16 | 11 | | |
| Category D | 21 | 8 | | |
+------------+---------+--------------+ | |
*/ | |
SELECT term.name,term.term_id,count(*) as result_count | |
FROM wp_terms as term | |
LEFT JOIN wp_term_relationships as tie | |
ON term.term_id = tie.term_taxonomy_id | |
LEFT JOIN wp_posts as post | |
ON post.ID = tie.object_id | |
LEFT JOIN wp_term_taxonomy as tax | |
ON term.term_id = tax.term_id | |
WHERE CONCAT_WS(post.post_content,post.post_title,post.post_excerpt) LIKE '%A%' | |
AND tax.taxonomy = "product_cat" | |
GROUP BY term.term_id | |
ORDER BY result_count DESC; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment