clusterBuddy
clusterBuddy

Reputation: 1554

Wordpress Advanced Custom Field not present in post object

My first and uttermost issue is that the_field('a_field_I_have_in_art_post') returns empty.

While debugging the post object, I'm not seeing any advanced custom fields.

I thought advanced custom fields appear as a meta key in the post object (?).

custom post type registry:

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');

post debugging:

$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;
        }
    }
}

Debug returns:

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) for instance returns empty, yet get_fields() in the post object loop shows a separate array of all ACF fields with no connection to the post object and its attributes (link, title, etc).

As seen in my snippet there, I created an assoc array to help with that, but that makes all my queries un-optemized, sluggish and very hard to combine with HTML with multiple nested foreach() especially for images as i'm trying to implement those images in bootstrap slider with multi inner-items based on post.

I am interested in using get_field(), each 'post_type' => 'art' has many images.

Upvotes: 0

Views: 2032

Answers (1)

tnog
tnog

Reputation: 420

I think that the issue is to understand what an ACF field is: simply, a WordPress custom field (stored in the wp_postmeta table), and not part of the standard WP_Post object that's stored in the wp_posts table.

Another answer that unpacks this in a little more depth, with some alternative approaches, is this answer in the WordPress Stack Exchange: https://wordpress.stackexchange.com/questions/172041/can-wp-query-return-posts-meta-in-a-single-request

So within your foreach loop above, you can retrieve your custom field using ACF's get_field($selector, $post_id), or you can also use get_post_meta($post_id, $key), but you'll need to pass the $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;
 }

Upvotes: 3

Related Questions