Manuel Hermida
Manuel Hermida

Reputation: 1

Show custom field from another CPT

I wanted to make a query: I have an entry in "assessment" CPT with custom fields made with ACF, one of which is name "customer" which has the customer ID. I want to know how to show in front-end a field of another CPT called "customers". The custom field I also did with ACF and is name "profile_photo". The author of this CPT is the same as the ACF custom field "customer" in "assessment" CPT.

I think it would be something like that ... I don't know if I'm on the right track.

function the_field_by_author( $field, $author ) {
  $user_info = get_field( "customer" ); 
  $args = [
    'post_author'    => $user_info->ID,
    'post_type'      => 'clientes', 
    'post_status'    => 'publish',
    'posts_per_page' => 1
  ];
  $my_post = get_posts( $args );
  if( $my_post ) {
    return get_field( "photo_profile", $my_post->ID );
  }
   }

add_shortcode('shortcode_the_field_by_author', 'the_field_by_author');

I'm new and I don't know how to display the custom field in each "assessment" entry. Thank you very much in advance.

Upvotes: 0

Views: 251

Answers (1)

iismaell
iismaell

Reputation: 633

The method is actually the same as what you would do with default post types such as posts. You can use the get_field or the the_field functions.

https://www.advancedcustomfields.com/resources/get_field/ https://www.advancedcustomfields.com/resources/the_field/

Just make sure to use the appropriate custom post ID in the function parameter.

$value = get_field( "text_field", 123 );

Make sure that 123 is the ID of a post from the custom post type.

Upvotes: 1

Related Questions