Create Post via Code

AGD-Andy

Member
I have a cron job that was on my old vBulletin forum that I want to convert. The steps in the process were like this:

* Query database for payments, ordered by date
* if there were NEW payments:
* Check Month - If there is not a thread for this month, create a new one. If there is, then create a new reply for that thread
* Mark new payments as submitted in DB

Essentially it created a new thread each month to list out payments so that everyone was always aware of payments made. Also created the thread so that users could add their own comments there.

What I don't know how to do with xenForo is how to create threads and replies via code :) Any starter-help would be greatly appreciated!
 
Use the datawriter:

PHP:
$writer = XenForo_DataWriter::create('XenForo_DataWriter_Discussion_Thread');
$writer->set('user_id', <userid of the poster>);
$writer->set('username', <username of the poster>);
$writer->set('node_id', <id of the node the thread is in>);
$writer->set('title', <thread title>);
$writer->set('prefix_id', <id of the thread prefix, if any>);

$postWriter = $writer->getFirstMessageDw();
$postWriter->set('message', <thread contents>);

$writer->save();

//Thread is saved.

This is a basic overview. Have a look at the library/XenForo/ControllerPublic/Forum.php file, line 518 (the actionAddThread function).
 
Use the post datawriter. Set the thread ID field.
From some searches, I assume it's this: XenForo_DataWriter_DiscussionMessage_Post

But how do I set the threadid? Sorry if this is documented somehow, I'm a recent convert from vB and haven't figured all this out yet.
 
From some searches, I assume it's this: XenForo_DataWriter_DiscussionMessage_Post

But how do I set the threadid? Sorry if this is documented somehow, I'm a recent convert from vB and haven't figured all this out yet.

In my above post, you see I create a data writer instance and then set things?

The first parameter of the set method is the field name, the second the value.

You must set message,userid,username,threadid for posts.
 
  • Like
Reactions: fly
I've used a slightly modified version from above example to include tag, the thread and post have been created in db and serialized tag is in the thread, but the tag row is not updated to include the latest_tagged_content.

What is the best way to update the tag row with new thread/post?

$writer = XenForo_DataWriter::create('XenForo_DataWriter_Discussion_Thread');
$writer->set('user_id', $user_id);
$writer->set('username', "xxxxx");
$writer->set('node_id', $node['node_id']);
$writer->set('title', "Initial Post");
$writer->set('tinhte_xentag_tags', serialize(Array('tag_text'=>$tag['tag_text'])));
$postWriter = $writer->getFirstMessageDw();
$postWriter->set('message', "Initial Post");
$writer->save();
 
I found the answer for anyone interested

$writer = XenForo_DataWriter::create('XenForo_DataWriter_Discussion_Thread');
$writer->set('user_id', $user_id);
$writer->set('username', "NSX News");
$writer->set('node_id', $node['node_id']);
$writer->set('title', "Initial Post");
$writer->Tinhte_XenTag_setTags(Array('tag_text'=>$tag['tag_text']));
$postWriter = $writer->getFirstMessageDw();
$postWriter->set('message', "Initial Post");
$writer->save();
 
Back
Top Bottom