Jump to content

Cannot get 'created' value after pages->add


michel v
 Share

Recommended Posts

Hi!

After adding a page with $pages->add(), the value of the returned page object's "created" property is always 0. I thought it would give me the timestamp of when the page was created.

In the admin, I can see that the page has been added and that the created/modified times are correct.

$testPage = $pages->add('basic-page', '/', [
  'title' => 'This is the page title',
  'body' => 'Lorem ipsum'
]);

var_dump($testPage->title);     // result: "This is the page title"
var_dump($testPage->created);   // result: 0

Is there a way to get the correct timestamp after adding a page with the API?

Thanks!

Link to comment
Share on other sites

The $page->created is empty at that very moment you query it because it hasn't been set yet.

<?php namespace ProcessWire;

$timestamp = time();

$page->setOutputFormatting(false);

$page->created = $timestamp;

// that quiet=true part is important in this case
$page->save(array('quiet' => true));

You could add the date on your own if the date is necessary for whatever reason in that moment.

  • Like 1
Link to comment
Share on other sites

7 hours ago, michel v said:

Is there a way to get the correct timestamp after adding a page with the API?

There are many ways ? What are you trying to do exactly and why? This helps finding the best solution ? Welcome to the forum btw!

  • Like 1
Link to comment
Share on other sites

Thanks for the welcome ?

Basically, I want to compare the "created" timestamp of the new page with the "created" timestamp of another page.

I have since found out that I can do it by calling $pages->get() with the id of the newly created page:

$testPage = $pages->add('basic-page', '/', [
  'title' => 'This is the page title',
  'body' => 'Lorem ipsum'
]);

$newPage = $pages->get($testPage->id);

var_dump($newPage->created);   // result: 1618991002

I don't really understand why I can't get at it directly though. If $testPage has an id, I would think it has been saved to the database, and all fields would be populated. But hey, whatever works ?

  • Like 1
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...