Is It Possible to Bulk Create Threads for Many WordPress Posts? SQL Maybe?

I have a WP site that has 1000ish old posts. I would like to bulk create Discourse comments threads for all of them. It appears that I can edit and save each post, and this would create a new comments thread if the settings are correct in the Discourse WP Plugin.

But… I’d rather not open and save 1k posts.

I can easily get the wp_post ids and titles that I need from the wp_post table. Maybe someone knows how to create a sql statement for Discourse that would create the new threads with the necessary data from wp_posts?


Are you using the WP-discourse plugin? If so I suspect you’re better off automating this in Wordpress.

(I don’t know how to do what you are asking specifically.)

1 Like

Yes, I am using the plugin, and it is set to automate, but in order for a post to “automatically” generate a comments thread in Discourse, the wp post needs to be updated. ei: opened-to-edit and saved.

Anyway, no, I feel that it would be far easier to query the data I need out of the wp mysql database, and populate it into the Discourse db with sql. I just don’t know the Discourse db at all, and hope that I can find someone who does.

I guess I could write a bash script for the wp cli to open / save all the posts. lol. Maybe I’ll do that.

1 Like

The problem is that your wordpress site wouldn’t have the necessary information to display the proper links, number of comments, and so forth if you do it on the Discourse side. I don’t have a solution for how to do it either side, but I do think you might want to focus your efforts on WP.


If the Wordpress command line tools allow you to iterate the posts and save them in a way that triggers the Discourse plugin to create the topics as you want, that’s certainly what I would do. It’s probably not very efficient but for a one-time job, any overhead will cost less time than trying to find a “better” solution.


As others have mentioned, the easiest way of doing this currently is via the WP Discourse plugin. Updating the dbs of each instance directly raises two potential issues:

  1. It requires you to understand all of the data and metadata being stored on each instance.
  2. The data and metadata is interdependent, i.e. after a post is successfully published from Wordpress to Discourse the id of the post in Discourse is saved to a post meta field in Wordpress.

If you were already familiar with both 1 and 2, then yes updating the dbs directly might be a good option, but considering you’re not, it’s not a good idea unless you want to spend time learning that just for this purpose. In that vein, you could hire someone to do this for you, however I would recommend you just use the WP Discourse plugin’s functionality instead.

Using the WP Discourse plugin has the additional benefit of logging already being setup for WP Discourse publishing, which means you’ll get detailed, post-specific information if any of the publications fail.

It’s true that this will mean 1000-odd POST requests to your Discourse as the WP Discourse plugin only POSTs one topic at a time, however considering this is a one-time migration, you can handle this by breaking it up into batches and through pauses (i.e. sleep) in the script. I would recommend doing a manual check after the first few batches to see it’s working as you expect.

In terms of the script itself, you would use the WPDiscourse\DiscoursePublish method publish_post_after_save for each post, i.e. in a loop (with appropriate batching and sleep).


Thanks to all!

I did not realize that there were db changes on both sides. Interesting. I am very familiar with the WP db. Too familiar I suppose. I often go to the db first when I should probably use other approaches.

Yes, I am such a db obsessed fool. I love db design and creation. But… I am not familiar with the Discourse db at all (yet). So…

Ah yes… proper logging is a wonderful thing.

Yes, excellent. I will consider this the proper answer, as it is precisely what I needed.


What I ended up doing was using the WP cli tool as follows

$wp post update 396 398 402 {several more here} --tags_input=discourse

Before this I retrieved a list of row ID’s from the wp_posts table that had post_status = ‘publish’ and post_type = post

I gave that list to the wp post command and it took about 500ms per row using a 4core server. If I specified more than 20 or so rows, Discourse would… ???.. but after that no more links would be made. So I fed it 20 at a time with a 30 second delay and worked on other projects.

So, that answer is a for-real hack, but for me (not yet having @angus answer), it was the most direct route.


This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.