高级自定义字段-查询效率

时间:2014-03-06 作者:Nate

出于测试目的,我编辑了页面。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])
我应该担心这个吗?

1 个回复
最合适的回答,由SO网友:Milo 整理而成

这会生成比您预期的更多的查询,有几个原因。

首先,ACF将字段存储为两个不同的数据段—一部分包含有关字段的信息、您选择的设置、输出格式等,另一部分包含分配给该字段的实际值。因此,每个字段都有一个额外的查询-字段数据加上字段的元数据。

另一个原因与您拥有的字段类型以及设置格式选项的方式有关。例如,图像字段似乎具有加载选定图像对象的选项。这意味着对于每个图像引用,ACF必须查询附件图像URL和元数据,这将为每个图像生成额外的查询。

你应该关心这个吗?

首先,应该注意的是,ACF的文件建议不要使用get_fields 如果您知道字段名和/或不会在每次页面加载时使用所有这些数据,因为函数使用的是低效的LIKE 查询以查找所有字段名称。

如果性能成为一个问题,您可以做以下几件事:1。使用缓存插件减少负载,和/或2。在数据更改时自己缓存数据。钩住acf/save_post 操作并将所有数据保存为单个选项中的数组,然后可以使用单个get_option 前端查询。

结束

相关推荐

使用选项控制jQuery插件

这可能是一个愚蠢的问题,但我一直在自学Wordpress,我一直在努力解决这个问题。我想做的是使用Wordpress控制面板页面来控制我网站上的倒计时时钟。我找到了一个jQuery倒计时,它似乎可以很好地工作,并且我已经学习了如何创建带有选项页的插件,以及如何让选项页与选项数据库对话。我在选项页面中创建了一个数组,其中包含倒计时应该结束的月份、日期和时间,并将其保存到选项数据库中的一个表中。我知道为了将数据输出到jQuery插件,我想使用wp\\u localize\\u脚本函数。然而,我似乎无法连接最后

高级自定义字段-查询效率 - 小码农CODE - 行之有效找到问题解决它

高级自定义字段-查询效率

时间:2014-03-06 作者:Nate

出于测试目的,我编辑了页面。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])
我应该担心这个吗?

1 个回复
最合适的回答,由SO网友:Milo 整理而成

这会生成比您预期的更多的查询,有几个原因。

首先,ACF将字段存储为两个不同的数据段—一部分包含有关字段的信息、您选择的设置、输出格式等,另一部分包含分配给该字段的实际值。因此,每个字段都有一个额外的查询-字段数据加上字段的元数据。

另一个原因与您拥有的字段类型以及设置格式选项的方式有关。例如,图像字段似乎具有加载选定图像对象的选项。这意味着对于每个图像引用,ACF必须查询附件图像URL和元数据,这将为每个图像生成额外的查询。

你应该关心这个吗?

首先,应该注意的是,ACF的文件建议不要使用get_fields 如果您知道字段名和/或不会在每次页面加载时使用所有这些数据,因为函数使用的是低效的LIKE 查询以查找所有字段名称。

如果性能成为一个问题,您可以做以下几件事:1。使用缓存插件减少负载,和/或2。在数据更改时自己缓存数据。钩住acf/save_post 操作并将所有数据保存为单个选项中的数组,然后可以使用单个get_option 前端查询。

相关推荐

如何读取WordPress$Query关联数组(散列)键的值

WordPress编程新手(来自更为传统的环境),并试图了解其一些“独特”特性。我们的网站上有一个目录页,此代码驻留在functions.php, 如果条件为true,则调整结果。if( $query->is_post_type_archive( \'directory\' ) ){ ...//do stuff } 我想知道如何获取is_post_type_archive 这就是“目录”当我对值使用测试时。。。var_dumb($query->is_post