Skip to content

Instantly share code, notes, and snippets.

@mgibbs189
Forked from SauntValerian/gist:a79845e301de969017e714a96ffee6da
Last active September 1, 2022 13:25
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mgibbs189/63ce2faf110b9021ca68e16a50955fc4 to your computer and use it in GitHub Desktop.
Save mgibbs189/63ce2faf110b9021ca68e16a50955fc4 to your computer and use it in GitHub Desktop.
Multiple Loops for FacetWP
<?php
$args = array(
'post_type' => 'location',
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title',
'facetwp' => true,
'tax_query' => array(
array(
'taxonomy' => 'advertiser-level',
'field' => 'slug',
'terms' => array(
'platinum',
'gold',
'silver',
'bronze'
),
),
)
);
$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, 'advertiser-level', array( 'fields' => 'slugs' ) );
$posts[ $terms[0] ][] = $post;
}
}
?>
<div class="entries">
<div class="facetwp-template">
<?php foreach ( $posts as $tier => $post_arrays ) : ?>
<div class="location_indv_post <?php echo $tier; ?>">
<?php foreach ( $post_arrays as $post_obj ) : ?>
<?php setup_postdata( $post_obj ); ?>
<!-- PLATINUM -->
<?php if ( 'platinum' == $tier ) : ?>
Paste this code: https://gist.github.com/SauntValerian/60566a02f9f4dc5fba79af38096d4f65#file-tier-sections-php-L4-L63
<?php endif; ?>
<!-- GOLD -->
<?php if ( 'gold' == $tier ) : ?>
Paste this code: https://gist.github.com/SauntValerian/60566a02f9f4dc5fba79af38096d4f65#file-tier-sections-php-L67-L105
<?php endif; ?>
<!-- SILVER -->
<?php if ( 'platinum' == $tier ) : ?>
Paste this code: https://gist.github.com/SauntValerian/60566a02f9f4dc5fba79af38096d4f65#file-tier-sections-php-L109-L130
<?php endif; ?>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
</div>
</div><!-- .entries -->
@rpasillas
Copy link

This gist just saved my bacon. Wow. Thank you so much.

@richerimage
Copy link

Epic!!!

@DavidPonsD
Copy link

How do I use this code? I do not understand very well where to put it, can you help me? Thank you

@Mark-Creeten
Copy link

this saved me too.
1 remark/tip, to get the content parts (the_title etc....) you have to pass in the $post_obj->ID as param

@lopadz
Copy link

lopadz commented Sep 18, 2019

🙌 freaking yes! Thanks!

@lopadz
Copy link

lopadz commented Jan 20, 2020

FYI, on line 29, I'm using get_the_terms() instead of wp_get_object_terms() for better performance (using the object cache).

@Mark-Creeten
Copy link

3 years later, found again this solution :-)
tip: if you don't need the default values from $post, like date created, modified, post-type, etc... you can skip the setup_postdata()

@Akaaal
Copy link

Akaaal commented May 6, 2022

Since Facetwp v4 you can add multiple templates per page by adding "static"
More explanations here = https://facetwp.com/help-center/listing-templates/listing-builder/#multiple-listing-templates-on-the-same-page

@lopadz
Copy link

lopadz commented May 6, 2022

Since Facetwp v4 you can add multiple templates per page by adding "static" More explanations here = https://facetwp.com/help-center/listing-templates/listing-builder/#multiple-listing-templates-on-the-same-page

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment