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

Thanks!

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.

or

  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.
2 Likes

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

Would similar code work for mapping wp_categories onto discourse_tags?

I’m living the “categories are walls” life :sunglasses:

Unfortunately there’s no equivalent wpdc_publish_post_tags filter at the moment.

What exactly are you trying to achieve with tags?

I have a custom post type of policies with a hierarchical custom taxonomy policy_categories on two levels (parent/child). There are about 45 child categories.

I’ve set WP Discourse up to publish to a Discourse category of policies, but rather than create a huge set of subcategories (having read around this forum) it seems better to create the subcategories as tags.

I’ll have various Wordpress users publishing policies with different experience levels (if it were only me doing this I’d just tag them every time). So to save on an extra step and probably a chunk of support hassle, ideally there’d be a filter to get WP Discourse to take the Wordpress Categories and add the matching Discourse Tags.

I’d be happy to make sure the categories and tags match IDs/slugs etc. as I’ll be holding any updates of these as admin.

Thank you for your help on this.

Maybe this is something for marketplace?

While there isn’t a dedicated tags filter, there is a general filter you can use: wpdc_publish_body. In your case it’ll look like this

add_filter(  'wpdc_publish_body', function( $body, $remote_post_type, $post_id ) {
   // code to get the post policy category
   if ( // if there is a post policy ) {
      $policy_tags = // map the post policy category to the appropriate tag(s)
      $body['tags'] = $policy_tags
   }
   return $body;
}, 10, 3);