Last active
February 14, 2020 01:24
-
-
Save wplit/4d7c2814b7f002d20d18582e1ce2003d to your computer and use it in GitHub Desktop.
Filter products in repeater to include only those in the same brand, while not including current post
This file contains 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 | |
add_action( 'pre_get_posts', 'lit_brand_filter' ); | |
function lit_brand_filter($query) { // Change lit_brand_filter to custom name, different each time you use | |
$taxonomy = 'brand'; // Change to taxonomy name | |
$terms = get_the_terms( get_the_ID(), $taxonomy ); | |
$tax_query = array( | |
'taxonomy' => $taxonomy, | |
'field' => 'slug', | |
'terms' => $terms[0]->slug | |
); | |
// Ensure current post isn't output | |
$current_id = get_the_ID(); | |
$query->set( 'post__not_in', array($current_id) ); | |
// Only get posts that have same taxonomy terms | |
$query->tax_query->queries[] = $tax_query; | |
$query->query_vars['tax_query'] = $query->tax_query->queries; | |
} | |
?> | |
Your repeater would be here, | |
<?php | |
remove_action( 'pre_get_posts', 'lit_brand_filter' ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment