• I am excited to have found this plugin because it provides the exact functionality that I need for a site I am developing. However, I’m struggling to get it working because I’m not an experienced WordPress developer or PHP coder and am not sure how to fill the gaps in the documentation.

    I’ve already created my custom post type using the ACF plugin. I’ve activated a custom plugin with the suggested PHP code, including the register_post_type() statement with what I think are appropriate values, but WordPress throws an error when I try to edit that post type.

    If I omit the register_post_type statement, nothing errors, but nor can I see any evidence of the plugin doing its work, if that means that a _connect taxonomy is supposed to be automatically created and populated.

    Do I still need to register the post type in my config code, and if so, how I ensure it does not conflict with its existing parameters? If I don’t need to include it, what else am I missing?

    I’d really appreciate your help. I gather that the plugin is targeted at experienced WordPress developers, but the functionality it provides is so valuable that it would be great if others can use it as well.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor philcable

    (@philcable)

    Hi @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_type statement. So chances are, the {post type key}_connect taxonomy is already created, and here’s what you can expect:

    • There won’t be an admin menu item for the {post type key}_connect taxonomy. (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 the array()).
    • 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.)
    • 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 Key from 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!

    Thread Starter angusveitch

    (@angusveitch)

    Thanks Phil, it was indeed already working, I just didn’t know where to look for the evidence. I can see checkboxes for tagging posts with the shadow terms. And having tagged some posts, I can add a filter facet (using WP Gridbuilder) to filter a query loop with the shadow terms. This is fantatstic, and gets me almost all the way to where I need to be.

    The other thing I want to do is create a query of posts based on a shadow term. So in the page template for my custom post type, I want to list all of the normal posts that have been tagged with the custom post’s shadow term. Is this doable?

    Plugin Contributor philcable

    (@philcable)

    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.
    Thread Starter angusveitch

    (@angusveitch)

    That seems to have worked – amazing! Thanks so much for that code.

    One odd glitch is that the pagination block lists lots of blank pages – perhaps it isn’t aware of the filter? Similarly, a WP Grid Builder facet can filter the results (which is fantastic!), but the result counts it shows next to the terms are not accurate, presumably because they reflect unfiltered results.

    Also, I don’t suppose there is a way to order the results by the term counts of the shadow terms? A quick google suggests that a custom filter can sort results by a meta field, but can we build a bridge to the shadow term properties?

Viewing 4 replies - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.