出于测试目的,我编辑了页面。php除了:
<?php while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php $site_options = get_fields(\'options\'); ?>
<?php endwhile; ?>
我删除了get\\u header、get\\u footer,只启用了ACF插件,因为我还处于开发早期,正在进行一些初始测试。当我在不调用get\\u字段的情况下查看页面时,它会报告9个查询,但如果我访问带有get\\u字段的页面,它会跳到72个查询。看起来应该只会增加1到2个查询。。。不是63。
查询报告使用:
get_num_queries();
get\\u选项调用的结果:
Array(
[main_ad_-_image] => Array
(
[id] => 25
[alt] => TEST
[title] => lg-social_media
[caption] =>
[description] =>
[mime_type] => image/png
[url] => {urlhere}
[width] => 460
[height] => 260
[sizes] => Array
(
[thumbnail] => {urlhere}/wp-content/uploads/lg-social_media-150x150.png
[thumbnail-width] => 150
[thumbnail-height] => 150
[medium] => {urlhere}/wp-content/uploads/lg-social_media-220x124.png
[medium-width] => 220
[medium-height] => 124
[large] => {urlhere}/wp-content/uploads/lg-social_media.png
[large-width] => 460
[large-height] => 260
)
)
[main_ad_-_url_type] => ext
[main_ad_-_url] => www.google.com
[small_ad_1_-_image] => Array
(
[id] => 28
[alt] => Fake Text
[title] => fake-text
[caption] =>
[description] =>
[mime_type] => image/png
[url] => {urlhere}/wp-content/uploads/fake-text.png
[width] => 220
[height] => 120
[sizes] => Array
(
[thumbnail] => {urlhere}/wp-content/uploads/sm-fake-text-150x120.png
[thumbnail-width] => 150
[thumbnail-height] => 120
[medium] => {urlhere}/wp-content/uploads/sm-fake-text.png
[medium-width] => 220
[medium-height] => 120
[large] => {urlhere}/wp-content/uploads/sm-fake-text.png
[large-width] => 220
[large-height] => 120
)
)
[small_ad_1_-_url_type] => ext
[small_ad_2_-_image] => Array
(
[id] => 30
[alt] => Fake Text 2
[title] => fake-text2
[caption] =>
[description] =>
[mime_type] => image/png
[url] => {urlhere}/wp-content/uploads/fake-text2.png
[width] => 220
[height] => 120
[sizes] => Array
(
[thumbnail] => {urlhere}/wp-content/uploads/fake-text2-150x120.png
[thumbnail-width] => 150
[thumbnail-height] => 120
[medium] => {urlhere}/wp-content/uploads/fake-text2.png
[medium-width] => 220
[medium-height] => 120
[large] => {urlhere}/wp-content/uploads/fake-text2.png
[large-width] => 220
[large-height] => 120
)
)
[small_ad_1_-_url] => www.google.com
[main_ad_-_page] => {urlhere}
[small_ad_2_-_url_type] => ext
[small_ad_2_-_url] => www.google.com
[small_ad_3_-_image] => Array
(
[id] => 27
[alt] => Fake Text 3
[title] => fake-text3
[caption] =>
[description] =>
[mime_type] => image/png
[url] => {urlhere}/wp-content/uploads/fake-text3.png
[width] => 220
[height] => 120
[sizes] => Array
(
[thumbnail] => {urlhere}/wp-content/uploads/fake-text3-150x120.png
[thumbnail-width] => 150
[thumbnail-height] => 120
[medium] => {urlhere}/wp-content/uploads/fake-text3.png
[medium-width] => 220
[medium-height] => 120
[large] => {urlhere}/wp-content/uploads/fake-text3.png
[large-width] => 220
[large-height] => 120
)
)
[small_ad_3_-_url_type] => ext
[small_ad_3_-_url] => www.google.com
[small_ad_4_-_image] => Array
(
[id] => 29
[alt] => Fake Text 4
[title] => fake-text4
[caption] =>
[description] =>
[mime_type] => image/png
[url] => {urlhere}/wp-content/uploads/fake-text4.png
[width] => 220
[height] => 120
[sizes] => Array
(
[thumbnail] => {urlhere}/wp-content/uploads/fake-text4-150x120.png
[thumbnail-width] => 150
[thumbnail-height] => 120
[medium] => {urlhere}/wp-content/uploads/fake-text4.png
[medium-width] => 220
[medium-height] => 120
[large] => {urlhere}/wp-content/uploads/fake-text4.png
[large-width] => 220
[large-height] => 120
)
)
[small_ad_4_-_url_type] => ext
[small_ad_4_-_url] => www.google.com
[facebook] => www.facebook.com
[twitter] => twitter.com
[youtube] => www.youtube.com
[office_address] => text
[office_address_-_google_url] => test
[office_-_phone] => 1234567890
[office_-_800_#] => 8001234567
[office_-_email] => [email protected])
我应该担心这个吗?
最合适的回答,由SO网友:Milo 整理而成
这会生成比您预期的更多的查询,有几个原因。
首先,ACF将字段存储为两个不同的数据段—一部分包含有关字段的信息、您选择的设置、输出格式等,另一部分包含分配给该字段的实际值。因此,每个字段都有一个额外的查询-字段数据加上字段的元数据。
另一个原因与您拥有的字段类型以及设置格式选项的方式有关。例如,图像字段似乎具有加载选定图像对象的选项。这意味着对于每个图像引用,ACF必须查询附件图像URL和元数据,这将为每个图像生成额外的查询。
你应该关心这个吗?
首先,应该注意的是,ACF的文件建议不要使用get_fields
如果您知道字段名和/或不会在每次页面加载时使用所有这些数据,因为函数使用的是低效的LIKE
查询以查找所有字段名称。
如果性能成为一个问题,您可以做以下几件事:1。使用缓存插件减少负载,和/或2。在数据更改时自己缓存数据。钩住acf/save_post
操作并将所有数据保存为单个选项中的数组,然后可以使用单个get_option
前端查询。