Jump to content

[SOLVED] Passing "parent page" to page creation via api


louisstephens
 Share

Recommended Posts

I am wondering, how do you pass a variable into wire('page')->get() inside a function? I have been looking through the forums, but unfortunately I have not found the answer yet.

My current set up is:

function generateNewPages($parentPageName) {
    $p = new Page();
    $p->template = "parent";
    $p->parent = wire('pages')->get('/home/');
    $p->name = $parentPageName;
    $p->title = $parentPageName;
    $p->of(false);
    $p->save();

    $p2 = new Page();
    $p2->template = "child";
    $p2->parent = wire('pages')->get('$parentPageName');
    $p2->name = "child";
    $p2->title = "Child Page";
    $p2->of(false);
    $p2->save();
}

When I try to run it by passing in a title like generateNewPages('Demo');, "Demo" is created, but when it gets to the child page I get:

Unknown Selector operator: '$' -- was your selector value properly escaped?

Is there a way to pass the $parentPageName to "wire('pages')->get('$parentPageName')" ?

Link to comment
Share on other sites

Just now, kongondo said:

Or remove them. You don't need them in this case ๐Ÿ™‚

Thanks everyone for the quick responses. Unfortunately when I do:

    $p2->parent = wire('pages')->get($parentPageName);

I get the following:

Exception: Unknown Selector operator: '' -- was your selector value properly escaped? (in MAMP/htdocs/development/pw/wire/core/Selectors.php line 378)

Im a little baffled as I thought it would work without the quotes as I dont see any glaring errors in Visual Studio Code.

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

ร—
ร—
  • Create New...