This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Combining Post Type and Taxonomy Grouping

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Combining Post Type and Taxonomy Grouping

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #52169
    melsaundersmelsaunders
    Participant

    Hello,

    I would like to group search results in a way that combines Post Type Grouping and Taxonomy Grouping, as follows:

    The first group should have the heading “Looking For” and display some Taxonomies.

    Below that, there should be separate groups for each Post Type (e.g., one group for posts, another for products, another for pages, etc.).
    (See Image)

    Could you please guide me on how to set this up ?

    Thank you!

    #52172
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    Well, that exact layout may not be possible as combining groupings is not possible.

    However there is still hope, by utilizing the “Other results” group.
    For that I would set it up like this: https://i.imgur.com/LllFNKT.png

    #52173
    melsaundersmelsaunders
    Participant

    Hello,

    Thank you for your response! I understand that combining groupings isn’t natively possible. However, I was wondering if this could be achieved with custom code, such as by editing the result templates or using filters/hooks?

    #52178
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Custom coding would be very complicated here I think.

    Have you tried the solution I suggested: https://i.imgur.com/LllFNKT.png
    That should be almost exactly what you want.

    #52189
    melsaundersmelsaunders
    Participant

    Yes, I tried the solution you suggested, and it works well! Thank you for that.

    I’d like to refine it further:

    Is it possible to add a link to the archive page for each post type at the end of its group?
    Can I reduce the number of results displayed for each group?

    #52208
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    Is it possible to add a link to the archive page for each post type at the end of its group?
    I’m afraid that’s not possible. I recall someone may have asked this very same question, it might be on the future features list, I am not sure. It is possible it may get added later on as a core feature.

    Can I reduce the number of results displayed for each group?
    Yes – under the Search Sources -> Limits panel, you can set the number of results for both taxonomy terms and post types: https://i.imgur.com/uHoyuvq.png
    If you want somewhat matching number of results for each post type, you can try this option as well, however it may cause some funky number of results in this case, so make sure to test it.

    #52212
    melsaundersmelsaunders
    Participant

    Thank you for the solution provided regarding limiting the number of results; I’ll give it a try!

    As for adding a link to the archive page for each post type at the end of its group, I experimented with the following code in the group-footer.php file, and it seems to be working fine:

    <span>
        <?php
        $post_type = get_post_type($r->id);
        if ($post_type === 'products') {
            // If the post type is 'products', add a link to the products archive page
            echo '<a href="' . home_url('/products') . '">See All Products</a>';
        } elseif ($post_type === 'post') {
            // If the post type is 'post', add a link to the articles archive page
            echo '<a href="' . home_url('/resources') . '">See All Articles</a>';
        } else {
            // Otherwise, just display the post type
            echo esc_html($post_type);
        }
        ?>
    </span>
    

    It seems to do the trick for now. Let me know if there’s anything I should be cautious about with this approach!

    #52221
    Ernest MarcinkoErnest Marcinko
    Keymaster

    That is actually perfect in my opninion. I was going to suggest the results templating as well, so this is definitely a very good solution.

    #52223
    melsaundersmelsaunders
    Participant

    Thanks so much for the feedback! Glad the solution works. I really appreciate your help!

    If anything else comes up, I’ll be sure to reach out. Thanks again!

    #52224
    Ernest MarcinkoErnest Marcinko
    Keymaster

    You are very welcome!

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.