我的一个页面上有以下代码。当我使用时,此页面显示100个查询CCD_ 1。我正在努力减少查询量,但我很难找到它们的来源。
我唯一能想到的就是get_post_meta
。get_post_meta每次都运行一个查询吗?如果是的话,有没有更好的方法可以做到这一点,这样我就不会每次都运行查询了?
<?php
/* This is the second wordpress loop which will show only non paid dealers per the above wp_query in query_single. The 0 passed is non-paid dealers*/
query_single('dealers', 'draft', '0', $taxtype, $value);
?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php
$address=get_post_meta($post->ID, 'wpcf-street_address', TRUE);
$city=get_post_meta($post->ID, 'wpcf-city', TRUE);
$state=get_post_meta($post->ID, 'wpcf-state_abbreviation', TRUE);
$zip=get_post_meta($post->ID, 'wpcf-zip_code', TRUE);
$phone=get_post_meta($post->ID, 'wpcf-phone_number', TRUE);
$paid=get_post_meta($post->ID, 'wpcf-paid', TRUE);
$post_id=get_the_ID();
get_each_dealer_brand($post_id);?>
<ul class="ullisting">
<?php
if($paid==0) {
echo "<li><p class='"plisting'"><strong>";the_title();echo "</strong></p></li>";
echo "<li><p class='"plisting'">$address | $city, $state $zip</p></li>";
echo "<li><p class='"plisting'">P: $phone</p></li>";
echo "<li><p class='"listing'"><span><small>$brands_list</small></span></p></li>";
}
?>
</ul>
您可以通过调用get_post_meta一次来减少查询次数,它将获取所有相关字段:
// will have all of your fields and more in an array, with fewer queries called
$post_meta = get_post_meta($post->ID);
var_dump($post_meta);
http://codex.wordpress.org/Function_Reference/get_post_meta