Automatically Select Topic Category Based on Post Category?

I intentionally have all the exact same categories in both WordPress and Discourse, but there’s no option to assign the post category as the topic category, so I just have to manually set it after each time I publish.

Seems like an obvious feature, so maybe I’m overlooking something?

I’m using the WP Discourse plugin.

1 Like

You can do this with the wpdc_publish_post_category filter. It’d be something like (untested example code, but may work out of the box):

function wpdc_change_post_category( $category, $post_id ) {
  $wp_category_name = get_the_category( $post_id )[0]->name
  $discourse_categories = WPDiscourse\Utilities\Utilities::get_discourse_categories();
  $discourse_category = array_search( $wp_category_name, array_column( $discourse_categories, 'name' ));
  return $discourse_category['id'];
add_filter( 'wpdc_publish_post_category', 'wpdc_change_post_category' );

Then, no matter what category you selected, the post would always be posted to a category with a name that matches the name of the Wordpress post category. Make sure those names match exactly though otherwise it won’t work.

Officially supporting this as a feature would be fraught with difficulty as it would rely on users maintaining lists of categories with the exact same names, assume that the category models between the two platforms remain the same and have to deal with the fact that Wordpress’ taxonomy system is inherently mutable (i.e. it won’t be the same on every Wordpress the plugin is installed on)

1 Like


You can do this with the wpdc_change_post_category filter. It’d be something like (untested example code, but may work out of the box):

I believe you mean wpdc_publish_post_category? wpdc_change_post_category doesn’t appear to actually exist. It’s just a custom function name that can be anything, correct?

$wp_category_name = get_the_category( $post_id )[0]->name is missing the closing ;.

After that, I get a too few arguments error concerning function wpdc_change_post_category( $category, $post_id ) {. It might be because $category is undefined? I may test more later.

Concerning adding this as an official feature, I don’t think it would be that big of a can of worms, considering two options:

  1. The Force Category Update option already exists. This could be extended to a feature that forces Discourse/WordPress categories to always be in sync.


  1. Maybe they don’t need to be in sync at all, but rather if a category doesn’t exist in Discourse, it simply creates it.

Yup that’s right, apologies for the typo.

There’s a big difference between reading a list of categories via the API (force category update) and automatically syncing categories themselves between Wordpress and Discourse.

1 Like