Support Forums

Looking for support? You can access the support system via your account.

Forums Forums Search & Filter Pro Group Results by Taxonomy

Viewing 5 posts - 1 through 5 (of 5 total)
  • Lars Faye
    #208843

    Hi there,

    I’m trying to do something that I thought was going to be simple, but seems to have tripped me up quite a bit. I’m trying to get the following layout:

    https://www.dropbox.com/s/z8teg3qd77w4qcd/sf-faqs.png?dl=0

    I have a Post Type (faq) with a custom tax (faq_category). I’m trying to group the results by their parent taxonomy. I am using the Custom results implementation and I can get it where I’m echoing the custom tax within the loop, but then the section/category shows for EVERY post within the loop (no bueno).

    If this was a standard loop, I would use a method such as this:

    https://wordpress.stackexchange.com/questions/32902/display-all-posts-in-a-custom-post-type-grouped-by-a-custom-taxonomy

    But I can’t quite figure out how to preserve the integrity of the S&F (especially the live Ajax filtering, the client LOVES that) while also modifying the query so that I’m looping through the terms and only pulling posts within that particular term.

    I’ve played with the S&F’s Query Filter, but to no avail. Would you be able to guide me in the right direction on this?

    Ross Moderator
    #208877

    Hi Lars, just to let you know we are on a national holiday in the UK (Easter), so replies / support will slower/delayed until we are back.

    Trevor Moderator
    #208946

    I think I understand. You want the results posts sorted by category. However our plugin does not sort by taxonomy terms. The reason is that you can have multiple tag/category/taxonomy terms on individual posts, and which term would you sort on? WordPress stores terms for tags/categories/taxonomies in arrays.

    A sort requires a single value field, so on some custom fields this can be done (but not on ones that can have multiple values). Be aware that if there is no value for a taxonomy/custom field the post will be omitted from the results entirely.

    I know that it is possible you may have only applied one category term per post, but it will still be stored as an array.

    If these values were in a custom field (post meta), then our plugin allows you sort by these, but then you would want titles and sections for each field value of the custom field in the results. So, the first step is to have a custom field with what you want to sort by, with single values for each posts, and then set this in our form in the sort settings.

    If your results page uses a PHP template (this would depend on the Display Results Method you are using and the theme or plugin you are using to output the results), it would mean creating some custom PHP to do this next part (sections and headings).

    As the while loop runs, before the post is output, the custom PHP would fetch the value of that custom field for the post and decide whether to output the header (if the value has changed from the previous post) with the custom field value in it.

    So, yes it is possible, but no, this is not included in our normal support.

    Lars Faye
    #209600

    Hi Trevor,

    Thanks so much! I understand this is outside your support and your suggestion on how I would theoretically was EXACTLY what I needed to make this happen. I was able to get this to happen following your suggestion.

    I saw some others that were trying to do this, so I wanted to post my code in case in might help someone else. This code is using the Custom query output, so this code is placed within my custom template. It’s commented and should hopefully make sense to someone who’s trying to achieve something similar. It should be noted that for this client, it made the most sense for the posts be sorted ASCENDING by DATE, rather than the default DESCENDING. I imagine if you wanted/needed to use DESCENDING, then you would need to base the foreach and conditionals off of next_post instead of prev_post:

    <div id="faqloop">
    
            <?php
            $args = array(
             'search_filter_id' => 8836
           );
            $faqs = new WP_Query($args);
            if ($faqs->have_posts() ) : $count=0;
              while ($faqs->have_posts() ) : $count++;
                $faqs->the_post();
    
                //Custom Fields
                $question = get_field( 'faq_question' );
                $answer = get_field( 'faq_answer' );
    
                // Adjacent Posts, IDs and Terms
                $prev_post = get_previous_post();
                $next_post = get_next_post();
                $next_post_id = $next_post->ID;
                $prev_post_id = $prev_post->ID;
                $prev_terms = get_the_terms( $prev_post_id, 'faq_category' );
                $terms = get_the_terms( $post->ID, 'faq_category' );
    
                // Create variable containers for the post IDs
                $term_slug = array();
                $prev_term_slug = array();
                $term_name = array();
    
                // Current tax loop
                foreach($terms as $term) {
                  $term_slug =  $term->slug;
                  $term_name =  $term->name;
                }
                //Adjacent Post tax loop
                foreach($prev_terms as $prev_term) {
                  $prev_term_slug =  $prev_term->slug;
                } ?>
    
    <?php //Echo term title if it's different than previous term OR is the last post in the loop
    if($prev_term_slug !== $term_slug || !$prev_post){ ?>
      <div class="faq-section--header <?php echo $term->slug; ?>">
       <h3><?php echo $term_name; ?></h3>
     </div>
    <?php } ?>
    
    <div class="faq-section--questions">
      <div class="faq-entry">
        <div class="faq-entry--question">
          <i class="faq-toggle"></i><span><?php echo $question; ?></span>
        </div>
        <div class="faq-entry--answer">
          <?php echo $answer; ?>
        </div>
      </div>
    </div>
    
    <?php endwhile; ?>
    <?php else: ?>
      <span data-search-filter-action='infinite-scroll-end'>No FAQs match that search or combination</span>
    <?php endif;?>
    <?php wp_reset_postdata(); ?>
    </div>
    Trevor Moderator
    #209650

    Thanks for sharing. I will close this thread for now.

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Group Results by Taxonomy’ is closed to new replies.