Jump to content

Programmatically select pages


maddmac
 Share

Recommended Posts

Is there a method to programmatically select pages from a PageSelect based on the name of the page and associate them?  I want to pass an array of page names or a comma separated list. These page names would be looped through and then added as multi page select for a specific field. These are existing pages not new pages.  It's too cumbersome to manually select multiple pages

Link to comment
Share on other sites

@bernhard no problem. I have a list of child pages that I want to associate with a page. For example I have a particular author that I want to programmatically associate book titles (pages) with an author what would be the best way of doing this programmatically without manually associating pages by the book title name? I can easily create a comma separated list of the books per Author name.

Douglas Adams (author-page)

These are all books (pages)

  • The Hitchhiker's Guide to the Galaxy
  • Life, the Universe and Everything (Hitchhiker's Guide to the Galaxy, #3)
  • Book title 3
  • Book title 4

 

 

Link to comment
Share on other sites

If you have a single page reference field called “author” on book pages (would probably recommend this way, even if you allow multiple authors per book):

$book = $pages->get('template=book, title="The Hitchhiker’s Guide To The Galaxy"');
$author = $pages->get('template=author, title=Douglas Adams');
$book->of(false);
$book->author = $author;
$book->save();

If you have a multi page field called “books” on author pages:

$author = $pages->get('title=Douglas Adams');
$books = $pages->find('title="The Hitchhiker’s Guide To The Galaxy"|"Life, the Universe and Everything"');
$author->of(false);
foreach ($books as $book)
    $author->books->add($book);
$author->save();

 

  • Thanks 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...