Reply To: Display category archives in search results

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Display category archives in search results Reply To: Display category archives in search results

Ernest Marcinko
Ernest Marcinko

Hi again!

Actually I have never though of the plural forms. I’m going to re-work grouping options within 2 updates, so I might as well add some kind of option to define singular-plural forms.

So, I have tried something and it work on my test server. The good news is, that it does not need any modification in the search code, so it’s kind of update proof. However this will only work with non-grouped result list. Add the following code to the active theme functions.php file:

add_filter( 'asp_pagepost_results', 'asp_add_categories_to_results', 1, 1 );
function asp_add_categories_to_results( $pageposts ) {
  $category_res_arr = array();

  foreach ($pageposts as $k=>$v) {
    // Get the post categories
    $post_categories = wp_get_post_categories( $pageposts[$k]->id );
    $cats = "";
    // Add each category as result to the result array
    foreach($post_categories as $c){
        $cat = get_category( $c );
        $new_res = new StdClass();
        $new_res->title = $cat->name;
        $new_res->content = "";
        $new_res->image = "";
        $new_res->author = "";
        $new_res->date = "";
        $new_res->link = get_category_link($cat->term_id);
        $category_res_arr[$cat->term_id] = $new_res;
  // Merge the posts and the categories together
  return array_merge($pageposts, $category_res_arr);

This basically gets every category for each post and appends it at the end of the posts results array. It should handle duplicates, so each category should appear only once.

I’m doing fine, thank you for asking 🙂 Actually, I’m going to Montenegro next wednesday for a week with my family. I can’t wait to get some rest from the computer. Norway sounds fantastic in these hot days. I’ve never been there, my sister once was, she said it’s a wonderful country. You will surely have a great time.

Anyways, let me know if this code works for you, or if you need something else.

Ernest Marcinko

If you like my products, don't forget to rate them on codecanyon :)