title: Magento - Loop through all products in category, change value and save
body: <p>I need to loop through all the products in a particular category, change a custom attribute and then save each product.</p>
  
  <p>Currently i have:</p>
  
  <pre><code>$_category = Mage::getModel('catalog/category')-&gt;load($_POST['id']);
  $all_products = $_category-&gt;getProductCollection();
  
  foreach ($all_products as $product) {
      $product = Mage::getModel('catalog/product')-&gt;load($product-&gt;getId());  
      $product-&gt;setCustomAttribute(xxxx);
      $product-&gt;save();
  }
  </code></pre>
  
  <p>Is this best practice and can it be improved for performance? The collections average around 15-20 products in each and i am getting a very slow loadtime or even the script timing out. </p>
  
  <p>I've used <code>set_time_limit(0);</code> to try and force it through but i imagine i can improve my code to stop me having to do this? Eventually the collections may have over 100-200 products each so need to find a good performance now</p>