• Resolved eric.7186

    (@eric7186)


    Can you give me an example of a 1:n relationship? Say an Event cpt that lists more than one field from Location cpt: street, city. I created a field in group Event called: event_location (relationship) filter by post type: location. Then created two fields in group Location: street (text), city (text). From an Event I can add a Location but I don’t know how to make the two fields show up. Do I need to enter some code into the functions.php file?

    • This topic was modified 1 year, 1 month ago by eric.7186.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Phi Phan

    (@mr2p)

    Hello @eric7186, You do need to put a custom code in the functions.php file to make that work. Here is the code:

    add_filter(
      'meta_field_block_get_acf_field', function ( $block_content, $post_id, $field, $raw_value ) {
        $field_name = $field['name'] ?? '';
    
        // Replace location_street with your unique name.
        if ('location_street' === $field_name ) {
          // Get post id for location post.
          $event_location_id = get_field('event_location', $post_id);
    
          // Has a value.
          if ($event_location_id ) {
            $block_content = get_field('street', $event_location_id);
          }
        }
    
        return $block_content;
      }, 10, 4
    );
    
    add_filter(
      'meta_field_block_get_acf_field', function ( $block_content, $post_id, $field, $raw_value ) {
        $field_name = $field['name'] ?? '';
    
        // Replace location_city with your unique name.
        if ('location_city' === $field_name ) {
          // Get post id for location post.
          $event_location_id = get_field('event_location', $post_id);
    
          // Has a value.
          if ($event_location_id ) {
            $block_content = get_field('city', $event_location_id);
          }
        }
    
        return $block_content;
      }, 10, 4
    );

    Because an event has only one location, I suggest you use a?Post Object field?for the event_location field. The code above is for the Post Object field. To display the location street and city in the event, you use two MFB blocks and set the field type for both as ACF and the field name for them as location_street and location_city. Please let me know if it works. If you need further help, feel free to discuss more here.

    Phi.

    Thread Starter eric.7186

    (@eric7186)

    Thank you Phi,

    That worked great for me when I updated my site. Now that I have this specific example I should be able to better understand the documentation.

    Sincerely Eric

    Plugin Author Phi Phan

    (@mr2p)

    @eric7186 You’re welcome Eric. I’m glad it worked for you.

    Regards, Phi.

    • This reply was modified 1 year, 1 month ago by Phi Phan.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘One to many relationship’ is closed to new replies.