1

I have setup fields and sub-fields using Advanced Custom Fields (ACF). The structure is similar to:

Field: Pet Details
- Sub-Field: Pet Name (text)
- Sub-Field: Pet Birthday (date picker)
- Sub-Field: Pet Gender (taxonomy, select)
- Additional... 

I have been able to use these within my custom post type (/cpt_slug/post_title) using the get_field(), get_sub_field() functions, however I have not been able to usilise the default ACF functions ( get_field(), get_sub_field(), etc ) in conjunction with additional pages where this information is being displayed - in this case, domain.com/account (woocommerce my account page) which is fine as I'm happy to use WP_Query.

While I have a working code below, it requires me to setup $variable = get_post_meta( $pet->ID, field_sub_field, true ) for each acf sub_field. Given the array structure,

The best result I could come up with was echo $details['pet_details_pet_name'][0] which outputs 'Cooper' (correct), but with my limited knowledge of arrays, is there any chance that the '0' will ever become a '1' and not allow this to work? I don't believe that the cpt/acf will ever put a second value in the array but I would like to hear your thoughts.

Code

global $current_user;

$query = new WP_Query ( array ( 
    'post_type' => 'audp_pets', 
    'author' => $current_user->ID, 
    'order' => 'ASC' 
    ) );

$pets = $query->posts;

if ( $pets ) {

    foreach ( $pets as $pet ) {

        var_dump ( $pet );

        ?><hr><?php

        echo "\$pet->post_title = " . $pet->post_title . '<br />';

        ?><hr><?php

        $details = get_post_meta( $pet->ID );

        $pet_name = get_post_meta( $pet->ID, 'pet_details_pet_name', true );
        echo "\$pet_name = " . $pet_name . '<br />';

        ?><hr><?php

        var_dump ( $details );


    }

wp_reset_postdata();

}

Outputs (Source)

object(WP_Post)#13185 (24) {
  ["ID"]=>
  int(952)
  ["post_author"]=>
  string(1) "1"
  ["post_date"]=>
  string(19) "2020-01-16 16:24:17"
  ["post_date_gmt"]=>
  string(19) "2020-01-16 05:24:17"
  ["post_content"]=>
  string(0) ""
  ["post_title"]=>
  string(6) "AA0AA0"
  ["post_excerpt"]=>
  string(0) ""
  ... (additional fields)
}

(N.B -- $pet->post_title = AA0AA0)

array(56) {
  ["_edit_last"]=>
  array(1) {
    [0]=>
    string(1) "1"
  }
  ["_edit_lock"]=>
  array(1) {
    [0]=>
    string(12) "1579152259:1"
  }
  ["pet_details_pet_name"]=>
  array(1) {
    [0]=>
    string(6) "Cooper"
  }
  ["_pet_details_pet_name"]=>
  array(1) {
    [0]=>
    string(19) "field_5e1189ab705b2"
  }
  ["pet_details_pet_birthday"]=>
  array(1) {
    [0]=>
    string(8) "20130313"
  }
  ... (additional fields)
}

(N.B. $pet_name = Cooper)

Thank-you in advance.

6
  • You could turn it into a normal the loop if($query->have_posts()): while($query->have_posts):the_post(); etc. then you set it up to be able to do it in a normal style. - Else you can also send the post id, to any ACF function and get the value - like this get_field('fieldname', $pet->ID) Commented Jan 16, 2020 at 7:16
  • if it is a repeater, then read the ACF documentation on repeaters, same with groups and layouts etc. it's all in the acf docs advancedcustomfields.com/resources/repeater Commented Jan 16, 2020 at 7:20
  • Thank-you @Stender. Can loops be used on 'pages' such as 'myaccount' or are they specific to 'posts'? I have been researching for days how to make acf functions including get_field() and get_sub_field()' working on this single-custom-post.php and nothing works. It works fine on my /custom-post-slug/page_title/` but not on this page. Furthermore, I found a number of articles stating that the number of requests were MUCH more with ACF than with WP_Query(). Commented Jan 16, 2020 at 7:26
  • @Stender I have tried loops also and that doesn't work. Nothing has allowed me to return a result from ACF functions such as get_field() or get_sub_field() in this page (yes I have get_header() and get_footer() included). Commented Jan 16, 2020 at 7:30
  • You are echoing the getters right? Commented Jan 16, 2020 at 7:38

1 Answer 1

1

Basically, You can do it like this :

global $current_user;

$args = array ( 
'post_type' => 'audar_pets', 
'author' => $current_user->ID, 
'order' => 'ASC' 
);

$query = new WP_Query( $args );

//Here we set the loop up so we can use it all in the normal way.. 
if($query->have_posts()){
    while ($query->have_posts()) {
        $query->the_post();
        echo get_the_title();
        //Get you fields
        var_dump(get_field('pet_details'));

        if( have_rows('pet_details') ):

            // loop through the rows of data
            while ( have_rows('pet_details') ) : the_row();

                // display a sub field value
                the_sub_field('pet_name');

            endwhile;

        else :

        // no rows found

    endif;
    } 
wp_reset_postdata();
}

But this is guessing that you are using a repeater and it is indeed a sub field, and not just another field..

You can also use the have_rows etc by adding the post id at the end, like you can with any ACF function.

You can get any field, from any page, post, attachment etc you want, by adding the id $value = get_field( "text_field", 123 );

Sign up to request clarification or add additional context in comments.

2 Comments

This is using wp_query to loop the posts. it really does have nothing to do with how you are getting the meta data - But since this sets up the post data, you can use $details = get_post_meta( get_the_ID(), 'pet_details_pet_name' ); var_dump($details) - If you have repeaters though, it might come back as a serialized array?
Fyi, ran the two different queries through query monitor. Pure wp_query and individual requests was 85 queries and the acf version was 100 (including core and theme queries). But nearly identical in their load times. Spoke to the author of a post from 2018 re acf slows websites with these queries and he stated they fixed the issues. It appears correct. Thanks again for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.