Jump to content

Trying to save multiple page reference fields.


timothy.m.wilson
 Share

Recommended Posts

Good afternoon,

I am attempting to loop through approximately 600 XML nodes. Each of those nodes has 2 child nodes (example below). One is an id for an existing page and the other contains values for a page reference field. I have put together a loop (below as well) to attempt to save each page with the label values. This all works....once. After getting the first XML node "label" saved to the correct page, nothing else is saved. I'm sure there is probably something that I have overlooked. But, I am also very new to this CMS. I'm hoping there is a veteran user who may be able to point me in the right direction. 

 

<row>
    <id>1041</id>
    <labels>lifestyle|savings</labels>
  </row>

 

foreach ($xml->row as $items) {
        
        //get page id to update and set output formatting to false
        $blog_page = $pages->get('id='.$items->id);
        $blog_page->of(false);

        //create array from pipe separated label values
        $labelsArray = explode('|', $items->labels);

        //loop through array to get page ids by page name
        $labelIDS = array();
        foreach ($labelsArray as $labelValue) {

            $labelPage = $pages->get('name='.$labelValue);
            array_push($labelIDS, $labelPage->id);
            
        }

        //add labels to label field in post
        $blog_page->labels = $labelIDS;
        
        //save page
        $blog_page->save();
    

}

Link to comment
Share on other sites

Wild guess, maybe it's only finding the first blog post, and not the others. I'm not sure $blog_page->labels = $labelIDS; works, since it's assigning plain arrays to a PageArray (as all page fields --set to accept multiple pages-- are).

Try this one. I replaced multiple calls to DB with $pages->findIds, which makes single call, so it's faster. I also replaced $blog_page->labels = $labelIDS; with PageArray::add($ids) method, which can take single id or array of ids (not documented but it works). As labels in your XML set as label1|label2, suitable as $pages selector, you dont need to split it.

Uncomment echo statements to see what pages are the issue

<row>
	<id>1041</id>
	<labels>lifestyle|savings</labels>
</row>

<?php 

foreach ($xml->row as $items) {
        
        //get page id to update and set output formatting to false
        $blog_page = $pages->get('id='.$items->id);
        if(! $blog_page->id) {
        	// echo for debugging
        	// echo "Cant find page with id $items->id <br/>";
        	continue;
        }

        // Get matching page ids at once, returns array of ids
        // https://processwire.com/api/ref/pages/find-i-ds/
        $labelIDS = $pages->findIds("name=$items->labels");

        if(! count($labelIDS)) {
        	// echo for debugging
        	// echo "Cant find labels $items->labels for page $items->id\n";
        	continue;
        }

        $blog_page->of(false);
        $blog_page->labels->add($labelIDS);
        $blog_page->save();
}

 

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