Jump to content

How to populate a page field via api?


Zahari M.
 Share

Recommended Posts

Hi Guys

Been trying to sink my teeth into the API as I want to be able to create pages via simple scripts.

I am using a page type field in my template which enables me to select other pages. I am selecting these other pages via checkboxes as a means to provide options for my page. i.e one option is to hide comments and the other is to hide the sidebar.

My problem is I can't populate the page type field with the values I need.

So currently I am doing this

$p = new Page();
$p->template = "basic-page";
$p->parent = "/api-generated-pages/";
$p->title = $randomTitle;
$p->save();

All this works great.

But how do I  modify the above script to also populate my page field named "pageoptions" with the page id values of say 1234 and 1235?

I've been searching the forum for an hour and tried many ways to write it but I can't seem to find a solution to set them. So pardon my poor searching. I'm sure the answer is there but I can't seem to find it!

Many thanks!

 

 

 

Link to comment
Share on other sites

Ok... I actually figured it out.

I just found this page which helped....

https://processwire.com/api/ref/page-array/add/

Here's what I ended up with....

$p = new Page();
$p->template = "basic-page";
$p->parent = "/api-generated-pages/";
$p->title = $randomTitle;
$p->pageoptions->add(array(1023,1026));
$p->save();

Fortunately I had read a few threads looking for the solution and that is what helped me add the array in to add multiple pages into the page field at once. 

Hope it helps someone else!

Have a great weekend everyone!

 

  • Like 5
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...