Support

Account

Home Forums Backend Issues (wp-admin) Display field from another CPT

Solving

Display field from another CPT

  • Hi,

    This is my case:

    My CPT#1 : Incident report
    (with fields like NAME of PART DAMAGED, …)

    My CPT#2: Reparation task
    with a relationship field to CPT#1 Incident report.

    As they are “linked”, in the WP admin listof CPT#2, how can i display a column with field from CPT#1 (NAME of PART DAMAGED) set in CPT#1 ?

    I hope i am clear enough, sorry for my bad english…
    thanks

  • You need to do a reverse relationship query in CPT2 to get the post for CPT1

    
    $args = array(
      'post_type' => 'CPT#2',
      'posts_per_page' => '1', // assuming there is only one
      'meta_query' => array(
        array(
          'key' => 'RELATIONSHIP_FIELD_NAME',
          'value' => '"'.$post->ID.'"',
          'compare' => 'LIKE'
        )
      )
    );
    $query = new WP_Query($args);
    if ($query->have_posts) {
      $related_id = $query->posts[0]->ID;
      // get field from the other post
      $value = get_field('some other field', $related_id);
    }
    

    see this page for more on reverse relationship queries https://www.advancedcustomfields.com/resources/querying-relationship-fields/

  • thanks a lot John, but is it possible to have these fields (from CPT#1) in the backend list (and in the CPT#2 form in read only) ? Using AdminColumnPro ?

    Should i also use https://wordpress.org/plugins/post-2-post-for-acf/

    thanks

  • I don’t know if my plugin will help or not. And I also can’t give any advice on the other plugin. You’re trying to display information from another post in the admin list, unless the other plugin gives you an option to do this then I would say no, even if you have my plugin installed. You need to find out from the other plugin if you can show data from a related post.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.