Filtering the available categories for a custom post type doesn't seem to work on blocks

I seen this but it seems it’s fixed?

<?php

add_filter( 'wp_discourse_publish_categories', 'my_namespace_filter_categories', 10, 2 );
function my_namespace_filter_categories( $categories, $post ) {
	if ( 'asbuilt-db' === get_post_type( $post ) ) {
		$output = [];
		foreach ( $categories as $category ) {
			if ( 'Databases' === $category['name']) {
				$output[] = $category;
			}
		}
		return $output;
	} else if ( 'applications' === get_post_type( $post ) ) {
		$output = [];
		foreach ( $categories as $category ) {
			if ( 'Applications' === $category['name']) {
				$output[] = $category;
			}
		}
		return $output;
	}

	return $categories;
}

Asbuilt DB part works
image

Applications part with new layout does not

Hey there @Fma965,

Could you just confirm for me that your custom post types have show_in_rest enabled?

See the example here

See also

Hey @angus

Yes REST API is enabled.

Using MetaBox.io

The filter should be working (I just tested it). Could you please try a simpler filter to see if it’s the filter or something else?

add_filter( 'wp_discourse_publish_categories', 'my_namespace_filter_categories', 10, 2 );
function my_namespace_filter_categories( $categories, $post ) {
	return array_filter( $categories , function( $c ) { return $c['name'] === 'Databases'; });
}
1 Like

Thanks, with this i managed to figure out what the issue is.

for reference i am using Advanced Scripts and if i set “location” to administrator area only it doesn’t work, it seems the code needs to run on either “everywhere” or “Front-end”
image

But the non blocks layout seems to work fine with Administration area, so i guess the Blocks interface is technically “front-end”

Problem solved thanks

EDIT: do you happen to know if this has been implemented? How to tag posts that are published from WordPress to Discourse - #4 by Fma965

Yup, because it’s consuming the REST API.

Yeah that was my thought process, which is why I changed that after you mentioned REST and confirmed it was working on your end :slight_smile: thanks again for you help.

1 Like