Last active
September 4, 2024 17:42
-
-
Save apsolut/c8c6c48d09b3644b3373f1ae88c2dc99 to your computer and use it in GitHub Desktop.
FacetWP group posts by taxonomy name and filter with custom facetwp filters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* based on https://gist.github.com/mgibbs189/63ce2faf110b9021ca68e16a50955fc4 | |
& based on https://gist.facetwp.com/gist/gist-63ce2faf110b9021ca68e16a50955fc4 | |
* Works with FacetWP | |
* Group things by taxonomy with taxonomy name before posts | |
* dont remove class names facet-wrap and facetwp-template | |
*/ | |
<div class="facet-wrap"> | |
<h1> | |
<?php esc_html_e('Page Title Here', 'wordpress'); ?> | |
</h1> | |
<section class="partners-filter"> | |
<?php echo facetwp_display('facet','country'); ?> | |
<?php echo facetwp_display('facet','partners'); ?> | |
</section> | |
</div> | |
<?php | |
$args = array( | |
'post_type' => 'YOUR-CPT', | |
'posts_per_page' => 99, | |
'hide_empty' => true, | |
'order' => 'ASC', | |
'facetwp' => true, | |
// 'tax_query' => array( | |
// array( | |
// 'taxonomy' => 'YOUR-TAX', | |
// 'field' => 'slug', | |
// ), | |
// ) | |
); | |
$posts = array(); | |
// Loop through the posts, storing data into the $posts array | |
$query = new WP_Query( $args ); | |
if ( $query->have_posts() ) { | |
while ( $query->have_posts() ) { | |
$query->the_post(); | |
$terms = wp_get_object_terms( $post->ID, 'YOUR-TAX', array( 'fields' => 'slugs' ) ); | |
$posts[ $terms[0] ][] = $post; | |
} | |
} | |
?> | |
<div class="entries"> | |
<div class="facetwp-template"> | |
<?php foreach ( $posts as $tier => $post_arrays ) : | |
$taxonomy_details = get_term_by( 'slug', $tier, 'YOUR-TAX' ); | |
//var_dump($taxonomy_details->name); | |
?> | |
<div class="segment <?php echo $tier; ?>"> | |
<h3><?php echo $taxonomy_details->name; ?></h3> | |
<?php foreach ( $post_arrays as $post ) : ?> | |
<?php setup_postdata( $post ); ?> | |
<div class="item"> | |
<?php the_title(); ?> | |
</div> | |
<?php endforeach; ?> | |
</div> | |
<?php endforeach; ?> | |
</div> | |
</div><!-- .entries --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
tax-preview-apsolut.mp4