Great, I’m glad it’s working for you!
I’m not familiar enough with WP Gridbuilder to speak to using it in this particular context, but listing the posts tagged with the custom post’s term is definitely doable with a bit more PHP. Functions are available for retrieving a post’s shadow term taxonomy and ID—both via the post ID, so they’re perfectly suited for use in a single post view template. Here are a couple quick examples for how you might implement it.
If you’re using a classic theme (non Full Site Editing), it’s fairly straightforward. You can add something like this directly in the single-{post type key}.php file:
$taxonomy = function_exists( 'ShadowTerms\API\get_taxonomy_slug' )
? \ShadowTerms\API\get_taxonomy_slug( get_the_ID() )
: '';
$term_id = $taxonomy ? \ShadowTerms\API\get_term_id( get_the_ID() ) : 0;
if ( $term_id ) {
$related = new WP_Query(
array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => $term_id,
),
),
)
);
if ( $related->have_posts() ) {
// Loop code (see https://developer.wordpress.org/reference/classes/wp_query/).
}
}
If you’re using a Full Site Editing theme, it’s a little more involved. You’ll need to add a Query block to the {post type key}-single.html file with some kind of flag—here, we’re going to use the namespace attribute (you can add the Query block first to make sure all the other parameters are what you want, then switch to code view to manually add the namespace attribute):
<!-- wp:query {"namespace":"shadow-terms/related","query":{"postType":"post","inherit":false}} -->
Then the query needs to be filtered. This code could go in the same custom plugin you made for adding Shadow Terms support:
add_filter( 'pre_render_block', 'filter_shadow_terms_query_block_pre_render', 15, 2 );
/**
* Filters the Query block to handle the shadow term "related posts" variation.
*
* @param string|null $pre_render The block content before it is rendered.
* @param array $parsed_block The block's parsed attributes.
*
* @return string|null The block content after it is rendered.
*/
function filter_shadow_terms_query_block_pre_render( ?string $pre_render, array $parsed_block ): ?string {
if ( 'shadow-terms/related' === ( $parsed_block['attrs']['namespace'] ?? false ) ) {
$post_id = get_queried_object_id();
$callback = function ( array $query ) use ( &$callback, $post_id ): array {
remove_filter( 'query_loop_block_query_vars', $callback );
$taxonomy = function_exists( 'ShadowTerms\API\get_taxonomy_slug' )
? \ShadowTerms\API\get_taxonomy_slug( $post_id )
: '';
$term_id = $taxonomy ? \ShadowTerms\API\get_term_id( $post_id ) : 0;
if ( ! $term_id ) {
$query['post__in'] = array( 0 );
return $query;
}
$query['tax_query'] = array(
array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => $term_id,
),
);
return $query;
};
add_filter( 'query_loop_block_query_vars', $callback );
}
return $pre_render;
}
Note that the site editor view of the template won’t display filtered results, but the front-end view should work.
I hope that helps!
-
This reply was modified 1 week, 2 days ago by
philcable.