Activate all languages on pages created via API

Problem

You have a multi-language setup and just created pages via api, but only the default language is activated on them.

Solution

If you want to activate all languages, use this after your page-creation API script. In this real-life scenario I had to import product pages, hence the template-selector below.

$pages->setOutputFormatting(false);
$pag = $pages->find("template='product'");
foreach($pag as $p) {
    foreach($languages as $lang) {
      if($lang->isDefault()) continue;
      $p->set("status$lang", 1);
      $p->save();
    }
}

Resources

Version

1.0.1

View/correct recipe on GitHub

https://github.com/processwire-recipes/Recipes/blob/master/activate-all-languages.md

Author(s)