Taxonomy terms block fails to find manually registered custom taxonomies

Hi everyone,

(I already contacted support, but I thought maybe someone here in the community has had a similar problem and can lend a hand as well :slight_smile: )

I’m struggling with the query block and taxonomy terms for custom taxonomies. I’ve registered a couple custom taxonomies with the default wordpress post type. Usually I would’ve registered them using ACF but the custom taxonomies come from a plugin called WP Recipe Maker and they’re not easily accessible by default. Therefore I had to register them manually using the following PHP snippet:

function register_recipe_taxonomies_for_parent_post() {
    // Only run this on the 'recipe' post type
    if ($post_type !== 'post') {
        return;
    }
    // Add the 'wprm_course' and 'wprm_cuisine' taxonomies to the 'post' post type
    register_taxonomy_for_object_type('wprm_course', 'post');
    register_taxonomy_for_object_type('wprm_cuisine', 'post');
}

add_action(‘save_post’, 'register_recipe_taxonomies_for_parent_post’);

I executed this snippet once by activating it and saving a post right after. Then I deactivated the snippet again.

Then I tried displaying these custom taxonomies using the cwicly taxonomy terms block. When I set the Source to “current” and select to include wprm_course or wprm_cuisine, the taxonomy terms block returns “Taxonomy Terms content not found”. Alternatively I tried displaying the custom taxonomy terms using the following PHP return function:

function get_post_recipe_type( $post_id ) {
    // Ensure we have a valid post ID
    if ( isset( $post_id ) ) {
        // Specify the taxonomy you are looking for (e.g., 'course_type' or 'countries')
        $taxonomy = 'wprm_course'; // Replace this with your actual taxonomy name

        clean_post_cache( $post_id );
        
        // Get the terms associated with the current post and this taxonomy
        $terms = get_the_terms( $post_id, $taxonomy );
        
        if (!empty($terms) && !is_wp_error($terms)) {
            return $terms[0]->name; // Return the first term's name
        } else {
            return 'No terms found or error fetching terms';
        }
    } else {
        return 'No post ID found';
    }
}

add_shortcode('post_recipe_type', 'get_post_recipe_type’);

When I use this function on the post itself (e.g. site_url/recipes/banana-nicecream) I’m able to display the custom taxonomy terms for the taxonomy wprm_course. But when I use the same function inside a query block on the post archive page (e.g. site_url/recipes) it always returns the same taxonomy term – even when that recipe does not have that term.

Interestingly enough, the taxonomy terms block yields no results at all in both scenarios (i.e. on the post itself vs inside query template).

I’m launching the website next week and I’d greatly appreciate some insight as to what I might be missing. Did I regsiter the custom taxonomies in a wrong way? I don’t think so because I started setting up a filter block and I was able to correctly filter the posts by the wprm_course taxonomy – so cwicly must have succesfully registered the connection between posts and the wprm_course taxonomy.

I’m really puzzled by the fact that some cwicly blocks do seem to have access to these manually registered custom taxonomies (i.e. the filter block) and others don’t (i.e. the taxonomy terms block). Any hint you can give me would help a lot already.

Cheers and thank you for your time
Marc

Hello @marc,

To start off, don’t you need to keep register_taxonomy_for_object_type active, otherwise I do believe it will only be applied that once during the save_post action.

I also believe that 'init' would be a good fit. Since you are only applying register_taxonomy_for_object_type to the post type post, I don’t believe you have to filter $post_type for other types.

1 Like

Hi @Louis ,

thanks for reaching out on such short notice!

Damn, I started out using the approach you suggested, but that wasn’t registering the taxonomies properly and I encountered a whole host of other issues, so I switched to the save_post trigger. That solved most of my issues, so I didn’t even question that it could the culprit for my problem with the taxonomy terms block.

I now switched back to 'init' and removed the post_type filter in my code. Result: the taxonomy terms block works as expected. Thanks for the tip @Louis , much appreciated!!

Cheers
Marc

2 Likes

Hello @marc,

Great news! Thanks for letting us know.

1 Like