XF 2.3 Thread Creator - Get new Thread's ID

nolatron

Member
I'm using the following code in a php script to generate a forum thread:

Code:
$dir = WEBSITE_PATH.'/forums';
require($dir . '/src/XF.php');
XF::start($dir);

$forumId = '20';  //Forum ID of where to post thread
$userId = '2';  //User ID # of account to post thread as
$title = "Testing";
$message = '[b]Name:[/b] Test';

$forum = \XF::em()->find('XF:Forum', $forumId);
$user = \XF::em()->find('XF:User', $userId);
\XF::asVisitor($user, function() use ($forum, $title, $message)
{
    $creator = \XF::service('XF:Thread\Creator', $forum);
    $creator->setContent($title, $message);
    $creator->setPrefix(1);  //User ID # of account to post thread as
    $creator->setIsAutomated();
    $creator->save();
});

What I'm trying to figure out is, is there a way to then get the new created thread's URL/ID so I can pass a link to another function?

Thanks.
 
The save method of the thread creator service returns the thread entity (\XF\Entity\Thread), so you can just use that.

PHP:
// ...
$thread = $creator->save();
$id = $thread->thread_id;
$url = $thread->getContentUrl();
 
Back
Top Bottom