XF 2.0 Programatically creating a thread with custom fields

Hi there,

I looked around and searched a lot to find information on this, but was unable to deduct how the
XF:Thread\Creator ->setcustomFields(array) works exactly

I've tried the following...
PHP:
$user = \XF::em()->find('XF:User', $data['user_id']);
$forum = \XF::em()->find('XF:Forum', $data['forum_id']);
\XF::asVisitor($user, function() use ($forum, $data) {
    $creator = \XF::service('XF:Thread\Creator', $forum);
    $creator->setContent($data['title'], $data['message']);
    $creator->setCustomFields([
           'mykeytest' => "testing value"
    ]);
    $creator->setIsAutomated();
    $creator->save();
});

But, unfortunately for me, this creates the thread with a blank value for the custom field with fieldId "mykeytest".

Could anyone help in answering what is the correct way to pass custom fields to the setCustomFields(array) parameter?

Thank you!
 
I have now realised that the code above works. However the visibility of the custom thread field was set to Admin only. I assume doing asVisitor() means that the account I post from would need Admin privs.

This thread can be closed. Hope the above is useful to anyone else in future.
 
Top Bottom