Created
August 21, 2023 13:30
-
-
Save Jehu/9ddbc4a39a3534fab72926456d8ddc79 to your computer and use it in GitHub Desktop.
filter Bricks term query for custom taxonomy tag
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 | |
// Include only terms where posts do have a term from custom 'hersteller' taxonomy | |
add_filter( | |
"bricks/terms/query_vars", | |
function ($query_vars, $settings, $element_id) { | |
if ($element_id !== "qaajzi") { | |
return $query_vars; | |
} | |
$hersteller_ids = []; | |
$term_id = get_queried_object_id(); | |
$args = [ | |
"post_type" => "produkt", | |
"post_status" => "publish", | |
"tax_query" => [ | |
[ | |
"taxonomy" => "category", | |
"field" => "term_id", | |
"terms" => $term_id, | |
] | |
] | |
]; | |
$my_query = new WP_Query($args); | |
if ( $my_query->get_posts() ) { | |
foreach($my_query->get_posts() as $post) { | |
$hersteller = array_shift(wp_get_post_terms($post->ID, 'hersteller')); | |
if($hersteller->term_id) { | |
$hersteller_ids[$hersteller->term_id] = $hersteller->name; | |
} | |
} | |
} | |
wp_reset_postdata(); | |
$query_vars["include"] = array_keys($hersteller_ids); | |
return $query_vars; | |
}, | |
10, | |
3 | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment