I have created two ACF Pro post types: Customers and Deliveries. Customers FG has the customer name as the post name, and fields for address, address2, phone, city and zip. Deliveries FG has a repeater called customer_delivery, which contains a Post Object called customer_po, and fields for special instructions, reason, pick up check, alternate address, temporary address and items.
What I am trying to do is create a Deliveries post that lists the data from the Customer based on the customer_po field and allows the entry of data into the Deliveries fields.
I used the Theme Code plugin to generate PHP, and had ChatGPT add in the fields from the Customer FC, and saved it in a PHP file in WPCodeBox. This the code it came up with:
<?php if ( have_rows( 'customer_delivery' ) ) : ?>
<?php while ( have_rows( 'customer_delivery' ) ) : the_row(); ?>
<?php $customer_po = get_sub_field( 'customer_po' ); ?>
<?php if ( $customer_po ) : ?>
<?php $post = $customer_po; ?>
<?php setup_postdata( $post ); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php wp_reset_postdata(); ?>
<?php $customer = get_field('customer', $customer_po); ?>
<?php $customer_address = get_field('address', $customer); ?>
<?php $customer_address_2 = get_field('address_2', $customer); ?>
<?php $customer_phone = get_field('phone', $customer); ?>
<?php $customer_city = get_field('city', $customer); ?>
<?php $customer_zip = get_field('zip', $customer); ?>
<?php echo $customer_address; ?>
<?php echo $customer_address_2; ?>
<?php echo $customer_phone; ?>
<?php echo $customer_city; ?>
<?php echo $customer_zip; ?>
<?php endif; ?>
<?php the_sub_field( 'special_instructions' ); ?>
<?php the_sub_field( 'reason' ); ?>
<?php if ( get_sub_field( 'pick_up_check' ) == 1 ) : ?>
<?php // echo 'true'; ?>
<?php else : ?>
<?php // echo 'false'; ?>
<?php endif; ?>
<?php if ( get_sub_field( 'alternate_address' ) == 1 ) : ?>
<?php // echo 'true'; ?>
<?php else : ?>
<?php // echo 'false'; ?>
<?php endif; ?>
<?php the_sub_field( 'temporary_address' ); ?>
<?php the_sub_field( 'items' ); ?>
<?php endwhile; ?>
<?php else : ?>
<?php // No rows found ?>
<?php endif; ?>
I have a Bricks Single template imported from ACSS/Frames (Contact Card Alpha). I’ve added in the ACF fields from the Dynamic Data. But all I can get to display is the sub-fields from the Deliveries entries. With the exception of the customers_po field, I can’t get the Customer fields to display.
What am I doing incorrectly here?
You say you’ve created post types, but then refer to them as field groups. The FGs are attached to the post types, yes?
Then it appears Deliveries has customer_po - which is the ID for the CPT CUSTOMER - to get the related customer.
But to me it looks like you get the post ID for the customer, and then get the customer again with
<?php $customer = get_field('customer', $customer_po); ?>
What are you getting here? Because you then use it to get the address.
<?php $customer_address = get_field('address', $customer); ?>
But shouldn’t this be:
<?php $customer_address = get_field('address', $customer_po); ?>
If all the fields are attached to the post?
Apologies if I have misunderstood the code. I feel this can be done with native bricks and no code, but I’ll leave that can of worms for now.
You say you’ve created post types, but then refer to them as field groups. The FGs are attached to the post types, yes? That is correct.
*But shouldn’t this be:
<?php $customer_address = get_field('address', $customer_po); ?>
If all the fields are attached to the post?*
I don’t know. I have a limited knowledge of PHP - it confuses me as much as JavaScript does.
I feel this can be done with native bricks and no code, but I’ll leave that can of worms for now.
I tried to do this with Bricks and no code, but the screencap is what I got - the Customer fields were not pulled in. If anyone has a suggestion that will bring them in without PHP, please let me know.
I feel like you approach might be improved, but obviously I’m not 100% clear on your structure. So I may be wrong, but I felt this might give you a better method/approach:
That didn’t help.
I did discover something, though.
I posted the following in a code block.
<?php if ( have_rows( 'customer_delivery' ) ) : ?>
<?php while ( have_rows( 'customer_delivery' ) ) : the_row(); ?>
<?php $customer_po = get_sub_field( 'customer_po' ); ?>
<?php if ( $customer_po ) : ?>
<?php $post = $customer_po; ?>
<?php setup_postdata( $post ); ?>
<?php $customer = get_field( 'customer', $customer_po ); ?>
<a href="<?php echo get_permalink( $customer ); ?>"><?php echo get_the_title( $customer ); ?></a>
<?php wp_reset_postdata(); ?>
<?php $customer_address = get_field('address', $customer_po); ?>
<?php $customer_address_2 = get_field('address_2', $customer_po); ?>
<?php $customer_phone = get_field('phone', $customer_po); ?>
<?php $customer_city = get_field('city', $customer_po); ?>
<?php $customer_zip = get_field('zip', $customer_po); ?>
<?php $customer_sic = get_field('sic', $customer_po); ?>
<p><?php echo $customer_address; ?>,
<?php echo $customer_address_2; ?></p>
<p><?php echo $customer_city; ?>
<?php echo $customer_zip; ?></p>
<p><?php echo $customer_phone; ?></p>
<p><?php echo $customer_sic; ?></p>
<?php endif; ?>
<?php the_sub_field( 'special_instructions' ); ?>
<?php the_sub_field( 'reason' ); ?>
<?php if ( get_sub_field( 'pick_up_check' ) == 1 ) : ?>
<?php // echo 'true'; ?>
<?php else : ?>
<?php // echo 'false'; ?>
<?php endif; ?>
<?php if ( get_sub_field( 'alternate_address' ) == 1 ) : ?>
<?php // echo 'true'; ?>
<?php else : ?>
<?php // echo 'false'; ?>
<?php endif; ?>
<?php the_sub_field( 'temporary_address' ); ?>
<?php the_sub_field( 'items' ); ?>
<?php endwhile; ?>
<?php else : ?>
<?php // No rows found ?>
<?php endif; ?>
All of a sudden the customer fields I was looking for appeared: address, address_2, etc. It seems that changing all “get_field” references from “$customer” to “$customer_po” brought the data in. (As you noted above!)
One problem: in the template builder, the “$customer_po” name/link shows correctly; in the front end, it shows the post name/link. That’s not usable.
But that’s not what I want to achieve, anyway. I did some searching, and found a link to this: SOLVED: Dynamic data calls to ACF relationship field no longer works - #12 by ainom
itchycode posted some screenshots that seemed to indicate that Dynamic Data inside a Text Element could be referenced and displayed when using Post Object fields. I would much rather use Bricks fields as opposed to manipulating the above code to format it.
Basically, what I’m asking is how do I change “{acf_address}” to render what I need as oppsed to using “<?php $customer_address = get_field('address', $customer_po); ?>” ?
It’s always so hard to answer having only half the story. But Bricks will return the ACF data for the current post.
If you build a page and add ACF fields, it has no idea which fields you want unless:
A: it’s a single post (or author/archive etc.) template - in which case it gets the fields from the current post (or authoer page etc.)
B: You add the fields in a query loop. The query goes and fetches the customer - and then acf_field_name will be fetched from that customer (or other post type).
The PHP works because the first thing it does it define the customer:
<?php $customer_po = get_sub_field( 'customer_po' ); ?>
Customer_po is the ID of the post. Are you using a template, or a query, to let bricks know which customer you want fields from?