<?php
// add package so xpdo can be used:
require_once '/home/foo/foo/foo/config.core.php';
require_once MODX_CORE_PATH.'model/modx/modx.class.php';
$modx = new modX();
$modx->initialize('web');
$modx->getService('error','error.modError');
$documents = $modx->getCollection('modResource',array('published' => 1, 'template' => '2'));
foreach ($documents as $document) {
$isbn = $document->getTVValue('bk-isbn');
$isbn = str_replace( ' ', '', $isbn);
$desc = $document->get('longtitle');
$ebkPrice = $document->getTVValue('ebk-price');
$ebkPriceUnder = $document->getTVValue('ebk-price-under');
$bndlPrice = $document->getTVValue('bndl-price');
$file = $document->getTVValue('ebk-file');
$rdate = $document->getTVValue('bk-reprintDate');
$pdate = $document->getTVValue('bk-pubDate');
$ebkoutput = $Products[] = "ebk$isbn,$desc - ebook,GBP=$ebkPrice,/home/foo/foo/$file,0,1440";
$bndloutput = $Products[] = "bndl$isbn,$desc - Bundle,GBP=$bndlPrice,/home/foo/foo/$file,0,1440";
}
}
return $ebkoutput;
return $bndloutput;
?>This question has been answered by markh. See the first response.

1 times select published resources with template 2
562 x 7 = 3934 times get TV value for TV with a specific name for this resource
$c = $modx->newQuery('modResource');
$c->where(array(
'published' => 1,
'template' => 2,
));
$c->innerJoin('modTemplateVarResource', 'TVISBN', 'modResource.id = TVISBN.contentid AND TVISBN.tmplvarid = 5'); // where 5 is the ID of the ISBN TV.
$c->innerJoin('modTemplateVarResource', 'TVPRICE', 'modResource.id = TVPRICE.contentid AND TVPRICE.tmplvarid = 6'); // where 5 is the ID of the Price TV.
// .. repeat
$c->select(array(
'modResource.*',
'isbn' => 'TVISBN.value',
'price' => 'TVPRICE.value',
//....
));
foreach ($modx->getIterator('modResource', $c) as $document) {
$values = $document->toArray('', false, true);
var_dump($values);
}Do you realize you are making 3935 SQL queries with that bit of code? 18 seconds sounds like you've got quite a beefy server running