使用高级自定义字段时,最小化Wordpress中的数据库查询

时间:2015-01-28 12:04:26

标签: mysql wordpress advanced-custom-fields

我在一个页面上工作,该页面列出了一家大公司的员工,并试图尽量减少我被迫查询数据库的次数,因为它变得非常复杂和缓慢。

  • 'person'是一种自定义帖子类型(大约有300人)
  • 'date_accredited'是通过高级自定义字段插件添加的日期字段。

只有经过认证的员工才能拥有'date_accredited'

我需要列出每个'person'但是首先列出所有认可的员工(因此大约有20名认可的员工排在最前面)。

目前,我正在调用WP_Query,如:

$args = array(
    'posts_per_page' => -1, 
    'post_type' => 'people', 
    'no_found_rows' => true, 
    'meta_key' => 'date_accredited'
);
$people = new WP_Query($args);

之后,我正在做:

while($people->have_posts()): $people->the_post();      
    $my_post_meta = get_post_meta($post->ID, 'date_accredited', true);
    if (!empty($my_post_meta)) {
        array_push($accredited, $post);
    } else {
        array_push($notAccredited, $post);
    }
endwhile;

让我们留下两个“人”阵列。对象。我的想法是,我可以做以下的事情来获得我想要的列表:

foreach($accredited as $person):
    personTile($person);
endforeach;

foreach($notAccredited as $person):
    personTile($person);
endforeach;

我试图避免重新查询数据库。

personTile();函数应该输出各种信息和HTML(the_post_thumbnail()以及各种高级自定义字段字段),但我现在意识到的是,这些都不包含在我从WP_Query()发布的帖子对象,所以我被迫使用以下内容:

  • get_the_post_thumbnail($person->ID)
  • get_permalink($person->ID)
  • get_field('date_accredited', $person->ID)

所有这些都需要另一个数据库查询(每个!),更糟糕的是,因为它们处于循环中,每个都会发生大约300次。

有没有办法让永久链接,缩略图和ACF字段包含在原始数据库查询中?我是否需要求助于自定义MySQL查询???

欢迎任何其他建议!

0 个答案:

没有答案