philcable
Forum Replies Created
-
Forum: Plugins
In reply to: [Shadow Terms] Help needed with initial setupGreat, 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}.phpfile:$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.htmlfile with some kind of flag—here, we’re going to use thenamespaceattribute (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.
Forum: Plugins
In reply to: [Shadow Terms] Help needed with initial setupHi @angusveitch!
You’re on the right track; since your custom post type is already created through the ACF plugin, you should indeed omit the
register_post_typestatement. So chances are, the{post type key}_connecttaxonomy is already created, and here’s what you can expect:- There won’t be an admin menu item for the
{post type key}_connecttaxonomy. (The plugin manages the terms, so this is by design—to prevent any conflicts or issues that manual maintenance might introduce.) - There will be a box/panel for the
{Post Type Plural Label}taxonomy on the edit screen for the post type(s) you added the taxonomy to (the ones in thearray()). - If the custom post type had published posts before Shadow Terms support was added, those posts won’t automatically populate terms—you’ll have to take a “priming” step for them.
- A bulk edit, even with no actual changes, is the quickest way to populate terms for existing posts. (Select all on your “All
{Post Type}” screen, select “Bulk Edit” from the “Bulk Actions” dropdown, click “Apply”, and “Update” without making any changes. Repeat as needed for page 2, etc.)
- A bulk edit, even with no actual changes, is the quickest way to populate terms for existing posts. (Select all on your “All
- New posts published after support was added will create a term—but only published posts.
Draft,Pending,Private, or any other status will not create a term.
One more note just for good measure: make sure you’re using the
Post Type Keyfrom the ACF post type in the support call:add_post_type_support(
'{Post Type Key}',
'shadow-terms',
array(...)
);Please let me know if that helps—especially if there’s a specific step that would be helpful to add to our documentation.
Likewise, if it still gives you trouble or you have further question, please don’t hesitate to ask!