Jump to content

Move position to first via API?


joe_g
 Share

Recommended Posts

Hi, is there a way to move the position of a page to first with the API?

I ran into an old problem: I'd like new pages to appear first, instead of last, but still retain manual drag-drop order – discussed in 2013:

I figure I could do a save hook that moves the page to the top, if there is a way via the API..

Link to comment
Share on other sites

If it is not necessary to sort the pages additionally manually the last one can also be the first one if you reverse the order.
Just go to the parent template settings select created in Sort settings for children and check Reverse sort direction

Link to comment
Share on other sites

How to set page->sort via API

The variable $page is the page you want to move

// move to beginning
$pages->sort($page, 0);

// move to end
$lastSibling = $page->siblings('include=all')->last();
$pages->insertAfter($page, $lastSibling);

// move 1 step forward
$pages->insertAfter($page, $page->next);

// move 1 step backwards
$pages->insertBefore($page, $page->prev);

// move to any index position
$newIndex = 0;
if ($newIndex === 0) $pages->sort($page, 0);
else {
    $futureBefore = $page->siblings('include=all')->eq($newIndex);
    $futureBefore = $futureBefore? $futureBefore : $page->siblings('include=all')->last();
    $pages->insertAfter($page, $futureBefore);
}
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...