I have this code fragment in my codebase:
foreach($this->adverisers as $adveriser) {
$this->data[$adveriser->name]['image'] = bfi_thumb(get_post_meta($adveriser->ID,
'ss_advertisers_cats_image', true ),
$this->imgDimensions
);
$this->data[$adveriser->name]['description'] = get_post_meta($adveriser->ID,
'ss_advertisers_cats_description',
true
);
$this->data[$adveriser->name]['advertiser'] = $wpdb->get_results( "SELECT DISTINCT * FROM {$wpdb->posts}
WHERE(post_type='brands' OR post_type='boutiques')
AND post_author='{$adveriser->post_author}' ", OBJECT
);
$this->data[$adveriser->name]['advertiserRedirectionLink'] = get_post_meta($adveriser->ID,
'ss_advertisers_cats_link',
true
);
}
The loop builds a custom array of data for later display on the site. As you can see, there is a total of four queries to the database executed on each iteration of the loop. This is probably far from optimal performance-wise as, in my view, this is an example of N+1 Query problem.
Is there any straightforward solution that would bring siginificant performance increase to this code? Or is my only option crafting a complex, muli-join SQL query with $wpdb
to get all the required data in one/two queries, or .. maybe the presented code is not so bad after all and would not hinder performance so considerably