Jump to content
Nico Knoll

Set "created" by creating a page via API

Recommended Posts

Hi,

is there a way to set the "created" date while creating a page via API?

I tried this:

$page->created = $data['crdate'];           // $data['crdate'] is an timestamp like 1321279318

But it won't work...

Greets,

Nico

Share this post


Link to post
Share on other sites

It works for me...

<?php
$page->created = time();
$page->save();

// outputs current timestamp
echo $page->created;

---

ops but after removing it outputs the old again, so it's not saved.

Share this post


Link to post
Share on other sites

I solved this with SQL:

First save normally and after this:

$sql = "UPDATE `pages` SET `created` = '".date('Y-m-d H:i:s', $data['crdate'])."', `modified` = '".date('Y-m-d H:i:s', $data['tstamp'])."' WHERE `name` = '".$page->name."';";

$update = wire('db')->query($sql);

Share this post


Link to post
Share on other sites

The $page->created and $page->modified properties are meant to be internal and consistent with actual page creation and modification dates in PW, so they aren't things that can be modified by the API. While you can go directly in and modify it with SQL, it's better to create your own properties using PW date fields. There's nothing that you can't do with your own fields that you can do with those internal created/modified fields. However, at present, I don't think it does any harm to modify them in SQL, but just saying that it's not a best practice.

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...