suntrop Posted February 26, 2017 Share Posted February 26, 2017 I cam across this issue a long time ago. When I create a page via API that page is always "created by" the guest user. I tried to add a dedicated user or the admin, but had no luck. Perhaps that field is – like created date – not API-able. $c = new Page(); $c->createdUser = wire('users')->get('name=admin'); Although I know that some fields won't be editable via API, I think this one is really confusing. For example, the first time I noticed this, was a client of mine, who nervously reported there is a protected page created by "someone from outside". It took me a while to get behind it :-D Link to comment Share on other sites More sharing options...
kongondo Posted February 26, 2017 Share Posted February 26, 2017 (edited) That's the normal behaviour (guest is created user for pages created via API). Edit: Changing created user via API requires one more step. Have a look here onwards: Edited February 26, 2017 by kongondo Link to comment Share on other sites More sharing options...
adrian Posted February 26, 2017 Share Posted February 26, 2017 If you really want the page's created user to be something else other than guest, you can do: $c = new Page(); $c->created_users_id = wire('users')->get('name=admin')->id; $c->save(array('quiet' => true)); Link to comment Share on other sites More sharing options...
suntrop Posted February 26, 2017 Author Share Posted February 26, 2017 Thanks guys! created_users_id works great … so I guess modified_users_id will work as well Link to comment Share on other sites More sharing options...
adrian Posted February 26, 2017 Share Posted February 26, 2017 7 minutes ago, suntrop said: Thanks guys! created_users_id works great … so I guess modified_users_id will work as well I haven't checked this in a long time, but in my experience, quiet mode won't work for modified, so I ended up doing this: $sql = "UPDATE `pages` SET `modified_users_id` = '".wire('users')->get('name=admin')->id."' WHERE `id` = '".$c->id."';"; $update = wire('database')->query($sql); And make sure it is after your original $c->save because each save will change the modified back. Perhaps the more important issue here, is why do you care about the name of the user that created/modified a newly created page? If it was created on the frontend via the API, then it was created by a guest user no? Or is the scenario different to this? Link to comment Share on other sites More sharing options...
suntrop Posted February 26, 2017 Author Share Posted February 26, 2017 Ha ha guessing isn't a good habit when programming. However, I just checked and this code will have my system user as created and modified $c->created_users_id = $this->systemUser; $c->modified_users_id = $this->systemUser; $c->save(array('quiet' => true)); 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now