Search code examples
phpwordpressadvanced-custom-fieldscustom-wordpress-pagesacfpro

I am trying to to find how to extract the object(WP_Post)# from each of my custom post types so that I can use the ACF get_fields() function


I am trying to to find how to extract the object(WP_Post)# from each $post of my $posts array in the function below so that I can assign it to a variable and add it to the parameter of the get_field() function. This is not a complete function, I have just been using var_dump to test.

<?php

function zip_search($userZip){
    $posts = get_posts(array(
     'posts_per_page'    => -1,
     'post_type'         => 'Location'
    ));


    if( $posts ) {

        foreach( $posts as $post ){
            //TODO: Get each posts ID and assign to a variable

            $zipField=get_field('zip_codes_serviced');

        }
        wp_reset_postdata(); 
    } 
}
?>

When I var_dump($posts); I get an array of 37 objects 1 for each of the location post types. I included one of the objects below:

object(WP_Post)#1758 (24) { ["ID"]=> int(1490) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2018-09-21 15:39:29" ["post_date_gmt"]=> string(19) "2018-09-21 15:39:29" ["post_content"]=> string(0) "" ["post_title"]=> string(14) "Scottsdale, AZ" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(13) "scottsdale-az" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2018-09-21 20:54:47" ["post_modified_gmt"]=> string(19) "2018-09-21 20:54:47" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(72) "http://dev-site.host.my/?post_type=location&p=1490" ["menu_order"]=> int(0) ["post_type"]=> string(8) "location" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" }

So I know that part of it works. I tried using the get_metadata() function but that does not seem to give me the information I need. Does anyone have any experience with this?


Solution

  • ACF's get_field function takes a Post ID as a second parameter:

    $zipField = get_field('zip_codes_serviced', $post->ID);