Mikie Posted April 8, 2019 Share Posted April 8, 2019 Hi there! I'm experiencing a weird problem creating pages via the api. My actual code is a bit more complicated, but the below summarises the issue. Would explicitly setting the name when creating a page cause this problem in anyone's experience? I am doing this for a reason, as I am creating thousands of pages through the api and the page titles have all sorts of characters that break selectors in them. I was experiencing issues both sanitising the titles (titles wrapped in quotations) and searching with sanitised titles, I thought creating a sanitised name variable would help my logic for checking for page existence and let me leave the titles as they were. $title = "Page Name"; $name = $sanitizer->pageName($title); $parent = $pages->get("name=parent"); $p = new Page(); $p->template = "template"; $p->parent = $parent; $p->title = $title; $p->name = $name; $p->save(); $test1 = $pages->get("name=$name,parent=$parent"); // NullPage $test2 = $pages->get("title=$title,parent=$parent"); // correct Page object Link to comment Share on other sites More sharing options...
flydev Posted April 8, 2019 Share Posted April 8, 2019 You could show us some titles to test it out. Did you read about how to work with the second argument there ? https://processwire.com/api/ref/sanitizer/page-name/ 1 Link to comment Share on other sites More sharing options...
dragan Posted April 8, 2019 Share Posted April 8, 2019 Page names have to be unique (if they share the same parent). If you create a new page with an already existing page-name, PW will append an integer. If you clone a page, it will add " (copy)" to the title, and also an auto-increment number to the page name. This happens both when you use the API or GUI. Maybe that's the problem here? You could check with slightly altering the selector for page name: 1 hour ago, Mikie said: $test1 = $pages->get("name%=$name,parent=$parent"); // NullPage name%= instead of name= 1 Link to comment Share on other sites More sharing options...
Mikie Posted April 8, 2019 Author Share Posted April 8, 2019 Hey @dragan and @flydev thanks for the responses. @flydev you solved it. My sanitised variable wasn't beautified and had multiple dashes to replace dirty characters, so the get was failing. Thanks so much for the help! 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now