帖子对象中不存在Wordpress高级自定义字段

时间:2018-10-20 06:38:18

标签: wordpress custom-post-type advanced-custom-fields

  • Wordpress 4.9.8
  • 高级自定义字段专业版5.7.7

我的首要问题是the_field('a_field_I_have_in_art_post')返回空。

在调试post对象时,我没有看到任何高级自定义字段。

我认为高级自定义字段在发布对象(?)中显示为元键。

自定义帖子类型注册表:

function art_init()
{
    $arts_labels = array(
        'name' => 'Arts',
        'singular_name' => 'Art',
        'menu_name' => 'Arts'
    );

    $args = array(
        'labels' => $arts_labels,
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => array('slug' => 'art'),
        'query_var' => true,
        'menu_icon' => 'dashicons-welcome-widgets-menus',
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            'trackbacks',
            'custom-fields',
            'comments',
            'revisions',
            'thumbnail',
            'author',
            'page-attributes',)
    );
    register_post_type('art', $args);
}

add_action('init', 'art_init');

调试后:

$posts = get_posts([
    'numberposts' => -1,
    'post_type' => 'art',
]);
if ($posts) {
    foreach ($posts as $postKey => $postValue) {
        var_dump($postValue->meta_key);
        $postAcfFields = get_fields($postValue);
        //var_dump('$postAcfFields: '.count($postAcfFields));
        if ($postAcfFields['item_images']) {
            $postObjWithImages[$postValue->ID] = $postAcfFields;
        }
    }
}

调试返回:

object(WP_Post)[1600]
  public 'ID' => int 1840
  public 'post_author' => string '1' (length=1)
  public 'post_date' => string '2018-10-13 10:53:45' (length=19)
  public 'post_date_gmt' => string '2018-10-13 10:53:45' (length=19)
  public 'post_content' => string '' (length=0)
  public 'post_title' => string 'this is a contact' (length=17)
  public 'post_excerpt' => string '' (length=0)
  public 'post_status' => string 'publish' (length=7)
  public 'comment_status' => string 'open' (length=4)
  public 'ping_status' => string 'open' (length=4)
  public 'post_password' => string '' (length=0)
  public 'post_name' => string 'this-is-a-contact' (length=17)
  public 'to_ping' => string '' (length=0)
  public 'pinged' => string '' (length=0)
  public 'post_modified' => string '2018-10-13 13:47:17' (length=19)
  public 'post_modified_gmt' => string '2018-10-13 13:47:17' (length=19)
  public 'post_content_filtered' => string '' (length=0)
  public 'post_parent' => int 0
  public 'guid' => string 'http://website.com/?post_type=art&p=1840' (length=61)
  public 'menu_order' => int 0
  public 'post_type' => string 'art' (length=3)
  public 'post_mime_type' => string '' (length=0)
  public 'comment_count' => string '0' (length=1)
  public 'filter' => string 'raw' (length=3)
例如,

the_field('item_title)返回空,而发布对象循环中的get_fields()显示了所有ACF字段的单独数组,与发布对象及其属性(链接,标题等)没有任何关系。

如我的片段所示,我创建了一个assoc数组来帮助解决此问题,但是这使我的所有查询都未经过优化,缓慢且很难与具有多个嵌套foreach()的HTML结合使用,特别是对于图像我正在尝试使用基于post的多个内部项目在bootstrap滑块中实现这些图像。

我对使用get_field()感兴趣,每个'post_type' => 'art'都有很多图像。

1 个答案:

答案 0 :(得分:1)

我认为问题是要了解ACF字段是什么:简单地,一个WordPress自定义字段(存储在wp_postmeta表中),而不是存储在wp_posts表中的标准WP_Post对象的一部分。

另一个可以用一些替代方法更深入地了解这个问题的答案是WordPress Stack Exchange中的这个答案:https://wordpress.stackexchange.com/questions/172041/can-wp-query-return-posts-meta-in-a-single-request

因此,在上面的foreach循环中,您可以使用ACF的get_field($ selector,$ post_id)来检索自定义字段,也可以使用get_post_meta($ post_id,$ key),但是您需要传递$ post_id。

$postAcfFields = get_fields($postValue); 
    //var_dump('$postAcfFields: '.count($postAcfFields));
    if ($postAcfFields['item_images']) { // This will always return false since you're not specifying the $post_id above in get_fields($postValue)
        $postObjWithImages[$postValue->ID] = $postAcfFields;
 }