$doc = $modx->newObject('modResource');
$doc->set('createdby', $user);
$doc->set('template', 2);
$doc->set('published', 1);
$doc->set('alias', $newtime);
$doc->set('pagetitle', $_POST['title']);
$doc->set('parent', 161);
$doc->set('content', $_POST['body']);
$doc->save();This question has been answered by BobRay. See the first response.
$modx->reloadContext('web');$cm = $modx->getCacheManager(); $cm->refresh();

$fields = array(
'createdby' => $userId,
'template' => 2,
'published' => 1,
'alias' => $newtime,
'pagetitle' => $_POST['title'],
'parent' => 161,
'content' => $_POST['body'],
);
$modx->runProcessor('resource/create', $fields);I think you might be seeing an alias map issue....
The first thing I'd try is putting this at the end of the code:
$modx->reloadContext('web');
That will recreate the alias map.
BTW, the preferred way create resources now is with the 'resource/create' processor.
BTW, $user is an ID right? (It needs to be.) You might consider calling it $userId to make that clearer. If you're going to write much MODX code, it will make your life easier if you use 'Obj' and 'id' as suffixes on any MODX object variables.
