# Automatically Select Topic Category Based on Post Category?

**URL:** https://meta.discourse.org/t/automatically-select-topic-category-based-on-post-category/229058
**Category:** WordPress
**Created:** [June 5, 2022, 11:12am UTC](https://meta.discourse.org/t/automatically-select-topic-category-based-on-post-category/229058 "2022-06-05T11:12:35Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![oopsyscoops](https://avatars.discourse-cdn.com/v4/letter/o/f14d63/32.png) [@oopsyscoops](https://meta.discourse.org/u/oopsyscoops)
#### Post date: [June 5, 2022, 11:12am UTC](https://meta.discourse.org/t/automatically-select-topic-category-based-on-post-category/229058/1 "2022-06-05T11:12:36Z")

</div>

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](https://github.com/discourse/wp-discourse) plugin.

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [June 6, 2022, 8:25am UTC](https://meta.discourse.org/t/automatically-select-topic-category-based-on-post-category/229058/2 "2022-06-06T08:25:27Z")

</div>

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):

```php
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.

> [@oopsyscoops](#):
>
> Seems like an obvious feature, so maybe I’m overlooking something?

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)

---

<div class="post-metadata">

### Author: ![oopsyscoops](https://avatars.discourse-cdn.com/v4/letter/o/f14d63/32.png) [@oopsyscoops](https://meta.discourse.org/u/oopsyscoops)
#### Post date: [June 7, 2022, 12:23pm UTC](https://meta.discourse.org/t/automatically-select-topic-category-based-on-post-category/229058/3 "2022-06-07T12:23:06Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [June 7, 2022, 12:45pm UTC](https://meta.discourse.org/t/automatically-select-topic-category-based-on-post-category/229058/4 "2022-06-07T12:45:36Z")

</div>

> [@oopsyscoops](#):
>
> 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?

Yup that’s right, apologies for the typo.

> [@oopsyscoops](#):
>
> 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.

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.

---

<div class="post-metadata">

### Author: ![carbonrich](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/carbonrich/32/274219_2.png) [@carbonrich](https://meta.discourse.org/u/carbonrich)
#### Post date: [October 6, 2022, 5:16pm UTC](https://meta.discourse.org/t/automatically-select-topic-category-based-on-post-category/229058/5 "2022-10-06T17:16:30Z")

</div>

Would similar code work for mapping wp\_categories onto discourse\_tags?

I’m living the “categories are walls” life 🕶

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [October 7, 2022, 2:29pm UTC](https://meta.discourse.org/t/automatically-select-topic-category-based-on-post-category/229058/6 "2022-10-07T14:29:27Z")

</div>

> [@carbonrich](#):
>
> Would similar code work for mapping wp\_categories onto discourse\_tags?

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

What exactly are you trying to achieve with tags?

---

<div class="post-metadata">

### Author: ![carbonrich](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/carbonrich/32/274219_2.png) [@carbonrich](https://meta.discourse.org/u/carbonrich)
#### Post date: [October 7, 2022, 6:13pm UTC](https://meta.discourse.org/t/automatically-select-topic-category-based-on-post-category/229058/7 "2022-10-07T18:13:14Z")

</div>

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](https://github.com/discourse/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](https://github.com/discourse/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.

---

<div class="post-metadata">

### Author: ![carbonrich](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/carbonrich/32/274219_2.png) [@carbonrich](https://meta.discourse.org/u/carbonrich)
#### Post date: [October 9, 2022, 5:17pm UTC](https://meta.discourse.org/t/automatically-select-topic-category-based-on-post-category/229058/8 "2022-10-09T17:17:11Z")

</div>

Maybe this is something for #Marketplace?

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [October 10, 2022, 7:33am UTC](https://meta.discourse.org/t/automatically-select-topic-category-based-on-post-category/229058/9 "2022-10-10T07:33:20Z")

</div>

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

```php
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);

```
