I couldn’t find anything pertaining to my question.
How difficult would it be to display the category of search results, without a plugin – if possible.
So it would be something like this:
Category One
Search results from category one
Category Two
Search results from category two
and so on.
Any help would be greatly appreciated. I don’t necessarily need anyone to do the work for me, I’m a PHP/Wordpress beginner, but I’d like to think I’m fairly competent in understanding code.
Unfortunately, I haven’t been able to find something that points me in the right direction when searching google and stackexchange.
What you basically want to do is to change the SQL query being done to group the result of the query by category
The code taken from here groups by performing a sort by category. You can place the following code in your themes search.php file
The rest of you search.php code should be something like
The display logic might be more complicated if posts can belong to more then one category as it will be more difficult to decide to which category you are switching
You could, in the search.php template page, first get the categories with get_categories.
Then in the loop, you could loop through the categories to see if the search result has that category. If so, push it under the index of the category. Here you wouldn’t display anything, just build you array.
Then after the loop you could use a foreach to display the search results ordered by your categories.
Like this, if a result is in more then a category, it would be displayed for each category.
UPDATE
The code would be something like this, inside search.php in your theme.
1) before the loop:
Gets the categories.
2) Inside the loop:
For each category, it checks if the current post has the category, if it does, it stores it in the array.
3) After the loop:
Maybe their are other ways to do this, this should work, although I haven’t actually tried it, so there might be some errors. If you do try it and it gives errors, let me know and I will try to help.