Jump to content

Recommended Posts

Posted

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();
	}
}
Posted

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!

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...