thetuningspoon Posted May 15, 2015 Share Posted May 15, 2015 This is something I thought would be pretty simple at first, but I'm having trouble figuring out the best way to accomplish it with PW's API: I want to look through all Page fields on my site and find all instances where they contain a certain page ID and replace it with a different page ID. Is there a way to do this directly with $fields or do I need to go through each $page and select the fields I want to modify by name? EDIT: Here's my first attempt at this. It seems pretty inefficient, though. // $p is the page with the id we want to replace // $newP is the page we're replacing it with // Create array of all the Page fields in the system $pageFields = wire('fields')->find("type=FieldtypePage"); foreach($pageFields as $pageField) { $pageFieldName = $pageField->name; $pageMatches = wire('pages')->find("$pageFieldName={$p->id}"); foreach($pageMatches as $pageMatch) { $pageMatch->$pageFieldName = $newP->id; // Replace the references with the new page $pageMatch->save(); } } Link to comment Share on other sites More sharing options...
thetuningspoon Posted May 18, 2015 Author Share Posted May 18, 2015 Oops, this should be in the API forum... The "attempt" in my previous post seems to be working. If anyone else has ideas on ways to improve this, I'd be interested to hear them, though! Link to comment Share on other sites More sharing options...
LostKobrakai Posted May 18, 2015 Share Posted May 18, 2015 Moved topic. 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