Posts not filtering within custom gutenberg block
-
I created a custom Gutenberg block to add a taxonomy query block. This block loops through a chosen taxonomy and display each term with the corresponding posts. I created this block template:
<?php
$block_id = '';
if ( ! empty( $block['anchor'] ) ) {
$block_id = esc_attr( $block['anchor'] );
}
$class_name = 'conversal-block-posts-by-taxonomy wp-block-group alignwide is-layout-flow wp-block-group-is-layout-flow';
if ( ! empty( $block['className'] ) ) {
$class_name .= ' ' . $block['className'];
}
if ( ! empty( $block['align'] ) ) {
$class_name .= ' align' . $block['align'];
}
$attributes = wp_kses_data(
get_block_wrapper_attributes(
array(
'id' => $block_id,
'class' => esc_attr( $class_name ),
)
)
);
// Get the current post type archive.
$post_type = get_post_type($post_id);
// Get the terms for the custom post type.
if ($post_type == 'afdeling') {
$terms = get_terms( array(
'taxonomy' => 'locatie', // Vervang 'category' met de juiste taxonomie als je een custom taxonomie gebruikt.
'hide_empty' => false,
) );
} elseif ($post_type == 'activiteit') {
$terms = get_terms( array(
'taxonomy' => 'activiteittype',
'hide_empty' => false,
) );
} ?>
<div <?php echo $attributes; ?>>
<?php if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
if ($post_type == 'afdeling') {
$args = array(
'post_type' => $post_type,
'tax_query' => array(
array(
'taxonomy' => 'locatie', // Vervang 'category' met de juiste taxonomie als je een custom taxonomie gebruikt.
'field' => 'term_id',
'terms' => $term->term_id,
),
),
'posts_per_page' => -1, // Pas aan naar behoefte.
);
$query = new WP_Query( $args ); ?>
<h2 class="wp-block-heading alignwide"><?php echo esc_html( $term->name ); ?></h2>
<div class="wp-block-query alignwide has-global-padding is-layout-constrained wp-block-query-is-layout-constrained">
<?php if ( $query->have_posts() ) { ?>
<ul class="columns-3 alignwide wp-block-post-template is-layout-grid wp-block-post-template-is-layout-grid">
<?php while ( $query->have_posts() ) { $query->the_post(); ?>
<li class="wp-block-post post-<?php echo get_the_id(); ?> afdeling type-afdeling status-publish hentry locatie-<?php echo esc_html( $term->slug ); ?>">
<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<h3 style="font-size:clamp(14px, 0.875rem + ((1vw - 3.2px) * 0.446), 18px);" class="has-link-color has-text-color has-donkerblauw-color wp-block-post-title">
<a class="has-link-color has-text-color has-donkerblauw-color" href="<?php echo get_the_permalink(); ?>" target="_blank"><?php echo get_the_title(); ?></a>
</h3>
</div>
</li>
<?php } ?>
</ul>
<?php } else { ?>
<div class="alignwide wp-block-query-no-results">
<p>Er werden geen afdelingen gevonden voor deze locatie</p>
</div>
<?php } wp_reset_postdata(); ?>
</div>
<?php } elseif ($post_type == 'activiteit') {
$args = array(
'post_type' => $post_type,
'tax_query' => array(
array(
'taxonomy' => 'activiteittype',
'field' => 'term_id',
'terms' => $term->term_id,
),
),
'posts_per_page' => -1,
);
$query = new WP_Query( $args ); ?>
<h2 class="wp-block-heading has-text-align-center" style="font-size:clamp(27.894px, 1.743rem + ((1vw - 3.2px) * 2.244), 48px);"><?php echo esc_html( $term->name ); ?></h2>
<div class="wp-block-query alignwide has-global-padding is-layout-constrained wp-block-query-is-layout-constrained">
<?php if ( $query->have_posts() ) { ?>
<ul class="columns-2 alignwide wp-block-post-template is-layout-grid wp-container-core-post-template-is-layout-2 wp-block-post-template-is-layout-grid">
<?php while ( $query->have_posts() ) { $query->the_post(); ?>
<li class="wp-block-post post-<?php echo get_the_id(); ?> activiteit type-activiteit status-publish has-post-thumbnail hentry activiteittype-<?php echo esc_html( $term->slug ); ?>">
<div class="wp-block-group activiteit-wrapper overflow-hidden position-relative animated fadeInUp has-wit-color has-text-color has-link-color is-nowrap is-layout-flex wp-container-core-group-is-layout-18 wp-block-group-is-layout-flex o-anim-ready" style="border-radius:12px">
<div class="wp-block-group wp-container-content-11 is-nowrap is-layout-flex wp-container-core-group-is-layout-16 wp-block-group-is-layout-flex">
<figure style="aspect-ratio:auto;" class="wp-block-post-featured-image">
<img width="520" height="347" src="<?php echo get_the_post_thumbnail_url() ?>" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" style="width:100%;height:100%;object-fit:cover;" decoding="async" loading="lazy">
</figure>
</div>
<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow" style="padding-top:40px;padding-right:30px;padding-bottom:40px;padding-left:30px">
<h2 style="margin-bottom:30px; font-size:clamp(17.905px, 1.119rem + ((1vw - 3.2px) * 1.127), 28px);" class="stretched-link wp-block-post-title">
<a class="has-wit-color has-text-color has-link-color" href="<?php echo get_the_permalink(); ?>" target="_self"><?php echo get_the_title(); ?></a>
</h2>
<div class="wp-block-post-excerpt">
<p class="wp-block-post-excerpt__excerpt"><?php echo get_the_excerpt(); ?></p>
<p class="wp-block-post-excerpt__more-text">
<a class="wp-block-post-excerpt__more-link has-wit-color has-text-color has-link-color" href="<?php echo get_the_permalink(); ?>"><strong>Meer info</strong></a>
</p>
</div>
</div>
</div>
</li>
<?php } ?>
</ul>
<?php } else { ?>
<div class="alignwide wp-block-query-no-results">
<p>Er werden geen activiteiten gevonden voor dit type</p>
</div>
<?php } wp_reset_postdata(); ?>
</div>
<?php } ?>
<?php } ?>
<?php } ?>
</div>I tried to use the same structure as the default query block but the filter doesn’t seem to filter the posts. Do I need to implement some other things for this to work or is there a better way to set this up?
You can find the working example here: https://raakvzw.be/afdelingen/
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- You must be logged in to reply to this topic.