1

I'm trying to display categories as search result items on top of the search.php results. There doesn't seem to be a standard WordPress function or WP Query to search through categories, so I figured I'd write my own MySQL statement.

SELECT DISTINCT t.*, tt.* 
FROM wp_terms AS t 
INNER JOIN wp_term_taxonomy AS tt ON t.term_id = tt.term_id 
WHERE tt.taxonomy IN ('category') AND (t.name LIKE 'hout') 
ORDER BY t.name ASC

In the above example, the search term is "hout". However this doesn't seem to return any results. Can you see anything wrong with my query?

Or do you know of another way I can include categories as search result items?

1 Answer 1

1

It sounds like you're looking for the get_terms() function that's a wrapper for WP_Term_Query.

There you can e.g. use the name__like, description__like or search parameters.

Example:

Here's what kind of WHERE query they generate:

'name__like'        => 'hout'  >>>  (t.name LIKE '%hout%') 

'description__like' => 'hout'  >>>  tt.description LIKE '%hout%'

'search'            => 'hout'  >>>  (t.name LIKE '%hout%') OR (t.slug LIKE '%hout%')
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.