Convert from num to datetime sorting

Is there a way to convert from num to datetime sorting?

In K2 I was sorting by number, not K3 has better date sorting, I want to use that, we have over 200 articles … Any way to easily convert?

Doesn’t that happen automatically once you change the sorting scheme? If not, you could programmatically unpublish and publish the pages again.

Honestly, I asked before trying. I am quite protective of that content folder

Make a backup copy, then just try…

Its not automatic, there example code to unpublish and republish everything that was already published? (we have drafts always)

image

In fact, that won’t work as will republish with todays date …

I was using a ‘created’ field to determine the date

created:
    label: Created
    type: date 

Needs to be published on this date

There is no example code, but looping through all pages and the within a try-catch block call $page->unpublish() und then in another loop $page->publish() should do the job…

Or maybe $page->sort() is the better option in this case if you then pass the field to sort by…

See my comment above, simple unpublish -> publish wont work as will publish on the wrong date.

I see no way from $page to get or set the date published?

First step: making pages unlisted:

<?php foreach(page('whatever')->children() as $p) {
  try {
      $p->changeStatus('unlisted');
   } catch (Exception $e) {
     echo $e->getMessage();

  }
} ?>

Then sort them according to date:

<?php foreach(page('whatever')->children() as $p) {
  try {
      $p->sort();
   } catch (Exception $e) {
     echo $e->getMessage();

  }
} ?>

With sorting in the child blueprint set to the date field.

Not sure in your example how the $p->sort() will work to sort and publish with the right date. Surely changeStatus(‘listed’) with the datetime num set will all publish to when the script was run?

The best way is probably to use changeStatus('listed')instead of sort() in your case.

How will changeStatus('listed') know what order to use and what date to put in the num? This is what I am lost on. Is this date obtainable within the template?

If you define the number scheme in the blueprint, the changeStatus() method should pick this up.