Skip to content

Instantly share code, notes, and snippets.

@wplit
Last active February 14, 2020 01:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wplit/4d7c2814b7f002d20d18582e1ce2003d to your computer and use it in GitHub Desktop.
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
<?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