Skip Navigation

[Resolved] how to update the PHP code in old template so related custom fields show

This thread is resolved. Here is a description of the problem and solution.

Problem: I recently migrated my existing post relationships and some custom code I had in place is no longer functioning as expected.

Solution: Use the new post relationships API instead of wpcf_pr_post_get_belongs, which is deprecated in the new system. Use the ID of the related post in the types_render_field function.

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/
https://toolset.com/documentation/customizing-sites-using-php/functions/

This support ticket is created 5 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 3 replies, has 2 voices.

Last updated by lauraH-2 5 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#1160817
working.png
not-working.png

When I updated my Toolset plugin, the relationship custom fields are no longer showing with my custom PHP template. I have a CPT called "Product Showcase" it is related to another CPT "Company". After the update, the custom fields from the Showcase are still showing, but the custom fields for the related Company are no longer showing. I checked the support and couldn't find out how to convert my php code. Just ignore the part that is creating a slider. Here is the code that needs to be updated:

<!---Product Showcase Area Begins Here-->
    <?php $posts = query_posts( $query_string . '&orderby=date&order=desc&post_type=product' ); ?>
    <?php if( $posts ) : ?>
    <h2 class="showcase">Product Showcase: <?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); echo $term->name; ?></h2>      
    <ul class="bxslider">
    <?php while(have_posts()) : the_post(); ?>      
     
      <li>
        <div class="slideInt">
          <div class="showLeft"><?php echo types_render_field ("product-image"); ?></div>
          <div class="showRight">
            <p class="company"><?php echo types_render_field("company-name", array( "post_id"=>'$company')); ?></p>
            <div class="phone"><?php echo types_render_field("main-phone-number", array( "output" => "html","post_id"=>'$company')); ?></div><div class="fullList"><a href="<?php $company_id = wpcf_pr_post_get_belongs(get_the_ID(), 'company'); echo get_permalink($company_id); ?>">Full Profile</a></div>
            <?php echo types_render_field("product-description"); ?>
          </div>
          <div class="clearAll"></div>
        </div>             
      </li>    
  <?php endwhile; ?>
    </ul> 
  <?php else : ?>
  <?php endif; ?>

Please let me now what to do so these related custom fields will show again. Thank you!

#1160898

Hi, if you used the migration tool in Toolset > Relationships to migrate to the new relationships system, this code should be updated. We have the new post relationships API documentation available here:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

I made some adjustments, and here's what I would like you to try:

<!---Product Showcase Area Begins Here-->
    <?php $posts = query_posts( $query_string . '&orderby=date&order=desc&post_type=product' ); ?>
    <?php if( $posts ) : ?>
    <h2 class="showcase">Product Showcase: <?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); echo $term->name; ?></h2>      
    <ul class="bxslider">
    <?php while(have_posts()) : the_post(); ?>      
      <?php $company = toolset_get_related_post(get_the_ID(), array('product', 'company'));?>
      <li>
        <div class="slideInt">
          <div class="showLeft"><?php echo types_render_field ("product-image", array("id"=>$company)); ?></div>
          <div class="showRight">
            <p class="company"><?php echo types_render_field("company-name", array( "id"=>$company)); ?></p>
            <div class="phone"><?php echo types_render_field("main-phone-number", array( "output" => "html","id"=>$company)); ?></div><div class="fullList"><a href="<?php echo get_permalink($company); ?>">Full Profile</a></div>
            <?php echo types_render_field("product-description", array("id"=>$company)); ?>
          </div>
          <div class="clearAll"></div>
        </div>             
      </li>    
  <?php endwhile; ?>
    </ul> 
  <?php else : ?>
  <?php endif; ?>

Let me know the results and we can go from there.

#1160948
Relationships ‹ TRSA Buyers Guide — WordPress.png

Hi Christian,
Thanks for replying so quickly! I added your code, but it removed everything from the Product Showcase and left it empty. I added the old "product-image" and "product-description" code back because they are coming from the Product Showcase CPT, and were showing originally, so there was no problem there. My problem is that the company name and phone number (from the Company post type) aren't showing. I did do the migration before I submitted the original ticket, and got the message in the attached image, so I assume the Product Showcase is the child post type and the Company is the parent post type. If it helps, you can see the item here (along with the Enhanced Listing CPT above it that I am also having problems with.) hidden link
I really appreciate your help!

#1160954

Hi Christian, I realized that you might have mixed up the parent - child relationship, so I just reversed them and viola! Thanks so much for taking the time to show me this. I would have never gotten it on my own. You're the best!
My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.