We have integrated Discourse into our Wordpress site, and posting manually works. I would however like to set up auto-posting to Discourse of only posts that fall into a specific category. I see we can exclude by tag, but that doesn’t help in this use-case. I would like to include by category. Is this possible and if not, could it be added as a feature? Seems like it would be an awfully useful/important option and honestly I was quite surprised to find it doesn’t appear to be there already. Thanks.
Hey @vanclute,
You can use the wpdc_publish_after_save
filter to achieve this. In your Wordpress functions.php
file add the following:
function publish_to_discourse( $force_publish, $post_id, $post ) {
return has_term( TERM, TAXONOMY, $post );
}
add_filter( 'wpdc_publish_after_save', 'publish_to_discourse', 10, 3 );
You’ll need to replace TERM and TAXONOMY with whatever you’re using for “categories” in Wordpress. Note that TERM can be an array, e.g.
array( 'category name 1', 'category name 2')
Example
function publish_to_discourse( $force_publish, $post_id, $post ) {
return has_term( array( 'news', 'blog' ), 'category', $post );
}
add_filter( 'wpdc_publish_after_save', 'publish_to_discourse', 10, 3 );
3 Likes
Thanks. Really would prefer to not have to modify any WP files but appreciate the pointers.
There is no need to modify anything Do it as it should do…
Install Code Snippet or similar and do that through it (sure, you can do it as a separate plugin if you prefer that; no differences). Totally same thing than installing a theme component or a plugin to Discourse.
And use functions.php of child theme only when modification is theme dependent. It is not the case this time.
1 Like