Skip Navigation

[Resolved] Need help converting code to new relationship method

This support ticket is created 4 years, 3 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 27 replies, has 3 voices.

Last updated by Minesh 4 years, 2 months ago.

Assisted by: Minesh.

Author
Posts
#1469705

Tell us what you are trying to do? I need to add another relationship to my code, but apparently, in order to do that, I need to update all the code to your new relationship method. Your plugin is telling me I have some issues with my existing code, and I'm having trouble figuring out what I need to do.

Is there any documentation that you are following? https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/backward-compatibility-measures-for-post-relationships/

Is there a similar example that we can see? This is my existing code for showing an individual location on the site. How can I make it compatible with this update?

<?php
$childargs = array(
'post_type' => 'class-link',
'numberposts' => -1,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(array('key' => '_wpcf_belongs_location_id', 'value' => get_the_ID()))
);
$child_posts = get_posts($childargs);

if (empty($child_posts)) {
print '<p>Stay tuned for more information</p>';
}

foreach ($child_posts as $child_post) {
$class_id = wpcf_pr_post_get_belongs($child_post->ID, 'class');

$class = get_post($class_id);

?>

What is the link to your site?

hidden link

#1470703

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Here is the guide how you can convert your existing post-relationship API code to a new relationship:
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/

In general, if you have a simple one-to-many relationship, You should use the Post Relationship API function toolset_get_related_posts to retrieve all related child posts belongs to specific parent.
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

For example:

$related_post_ids = toolset_get_related_posts(
                                    get_the_ID(),
                                    'class-link',
                                     'parent',
                                     100,
                                      0,
                                      array(),
                                     'post_id',
                                     'child'
                                     );

I do not know what relationship you have with your post type, I suggest you should go through the Doc I shared and if you need further assistance please feel free to get in touch with me.

#1471513

I already went over both the links you sent (before I contacted you) and do not understand what I need to do. This relationship is many-to-many. I have a "class" post type and a "location" post type. They are connected by a "class-link" post type, which is how this used to work. The "class-link" is what creates the relationship.

All I need to know is how to change this part of the query:
'meta_query' => array(array('key' => '_wpcf_belongs_location_id', 'value' => get_the_ID()))

And how to change this:
foreach ($child_posts as $child_post) {
$class_id = wpcf_pr_post_get_belongs($child_post->ID, 'class');
$class = get_post($class_id);

Once I have that, I should be able to apply it throughout the rest of my code.

Even as an experienced developer, this is confusing as I have not used this code in a while.

#1472927

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Have you migrated your site to new relationship?

Can you please share admin access details so I can check the current status of the relationship and where you see the message that says you have some issues with my existing code.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1490209

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I see you did not migrate to a new relationship yet and to run the migration I need a test/staging site.
=> hidden link

Is the access details you just shared with me is staging site? If no, Can you please share one.
- Also, as you said "Your plugin is telling me I have some issues with my existing code", where you see this notice?

#1490387
vtadultcte-relationships.jpg

This is not a staging site. I will have to make one. The attached image shows the message I get when I hit "Run the Migration."

#1491719

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - as its not a staging site, we do not recommend any kind of debugging on live site.

Can you please share a staging site access details and I will try to migrate the relationship to a new relationship and will try to adjust the code accordingly.

I have set the next reply to private which means only you and I have access to it.

#1494177

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

When I try to access the wp-admin with the staging site you shared, It asks me for authorization access details.

Can you please send me authorization access details.

I have set the next reply to private which means only you and I have access to it.

#1495949

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Thank you for sharing the authorization access details again.

Just would like to double-check again - you have many-to-many relationship created between the post type Classes and Locations - correct?

#1496347

That is correct!

#1497879

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now: hidden link

I've first migrated the relationship:
=> https://toolset.com/2018/05/when-if-to-migrate-relationships-in-old-toolset-sites/

As old relationship uses two one-to-many relationships to create many-to-many relationships. Once I migrated to new relationship I've two one-to-many relationships. So, to create many-to-many relationships, I've to merge those two one-to-many relationships.

This is how you can merge two one-to-many relationships to create many-to-many relationship:
=> https://toolset.com/documentation/post-relationships/how-to-merge-existing-post-relationships/

Now, regarding the code, I see you have used the custom code at many places:

Theme files	file: /city-of-wp-child/taxonomy-class-category.php - line 121, column 39	_wpcf_belongs_class_id', 'value' =>
Theme files	file: /city-of-wp-child/taxonomy-class-category.php - line 130, column 48	_wpcf_belongs_post-type-slug_id
Theme files	file: /city-of-wp-child/taxonomy-class-category.php - line 131, column 50	_wpcf_belongs_band_id', true);
Theme files	file: /city-of-wp-child/taxonomy-class-category.php - line 127, column 20	wpcf_pr_post_get_belongs($child_pos
Theme files	file: /city-of-wp-child/single-class.php - line 111, column 39	_wpcf_belongs_class_id', 'value' =>
Theme files	file: /city-of-wp-child/single-class.php - line 120, column 17	wpcf_pr_post_get_belongs($child_pos
Theme files	file: /city-of-wp-child/archive-class.php - line 112, column 39	_wpcf_belongs_class_id', 'value' =>
Theme files	file: /city-of-wp-child/archive-class.php - line 121, column 48	_wpcf_belongs_post-type-slug_id
Theme files	file: /city-of-wp-child/archive-class.php - line 122, column 50	_wpcf_belongs_band_id', true);
Theme files	file: /city-of-wp-child/archive-class.php - line 118, column 20	wpcf_pr_post_get_belongs($child_pos
Theme files	file: /city-of-wp-child/single-location.php - line 121, column 39	_wpcf_belongs_location_id', 'value'
Theme files	file: /city-of-wp-child/single-location.php - line 130, column 17	wpcf_pr_post_get_belongs($child_pos

That means, you have custom code added to the following files:
- taxonomy-class-category.php
- single-class.php
- archive-class.php
- single-location.php

I do not know yet why you created single-location.php + single-class.php theme files and added the custom code to it as you can easily create a view to display those records.

I would like to know now, do you still want to go with custom code where you want to replace the old custom cod with new relationship custom code or you would like to go with views where ever we can apply it?

#1505583

If it's not too difficult, I'd love to keep the old code. Otherwise, I feel like it will be a lot of unneeded work to build them again in views. I didn't really intend to change this code, but I have to build new functionality on the site. I don't actually need single-class.php because I'm not using it. But if there is code I can switch out on the other three pages, that would be great.

#1507031

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

If you are not using the - single-class.php file - you should simply remove it. How you are displaying the single for class post type?

I would like to know from the following list - which file you are using currently?
- taxonomy-class-category.php
- single-class.php
- archive-class.php
- single-location.php

#1507893

I'm not displaying single classes. I only display them in a list. So, I'm using these three:

- taxonomy-class-category.php
- archive-class.php
- single-location.php

#1508735

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

So, you can use the following WP_Query to retrieve the related posts using the new relationship:

On your single-location.php file you should replace the following code:


<?php 
$childargs = array(
'post_type' => 'class-link',
'numberposts' => -1,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(array('key' => '_wpcf_belongs_location_id', 'value' => get_the_ID()))
);
$child_posts = get_posts($childargs);

if (empty($child_posts)) {
    print '<p>Stay tuned for more information</p>';
}

foreach ($child_posts as $child_post) {
    $class_id = wpcf_pr_post_get_belongs($child_post->ID, 'class');

    $class = get_post($class_id);

 ?>
         
<!-- <div class="class-listing"> -->
        <ul>
        <li style="font-size: 18px;padding: 10px;background-color: #fed00c;color: white;padding-left: 15px;font-weight: 600; margin-bottom:3px;"><?php echo $class->post_title; ?></li>
        <?php //echo types_render_field( "book-cover", array( "id"=> "$child_post->ID", "size" => "thumbnail" )); ?>
    </ul>
   <!-- </div> -->
 
 
<?php } ?>

With the following one:

$query = new WP_Query( 
 array(
  'post_type' => 'class',
  'posts_per_page' => -1,
  //new toolset_relationships query argument
  'toolset_relationships' => array(
   'role' => 'parent',
   'related_to' => get_the_ID(),
   // this will work only with relationships that have existed before the migration
   // if possible, use the relationship slug instead of an array
   'relationship' => 'class-location',
    
  ),
   
 )
);
$child_posts = $query->posts;

if (empty($child_posts)) {
    print '<p>Stay tuned for more information</p>';
}

foreach ($child_posts as $class) {
   
 ?>
         
<!-- <div class="class-listing"> -->
        <ul>
        <li style="font-size: 18px;padding: 10px;background-color: #fed00c;color: white;padding-left: 15px;font-weight: 600; margin-bottom:3px;"><?php echo $class->post_title; ?></li>
        <?php //echo types_render_field( "book-cover", array( "id"=> "$child_post->ID", "size" => "thumbnail" )); ?>
    </ul>
   <!-- </div> -->
 
 
<?php } ?>

Here is the related Doc:
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#wp_query-argument-for-querying-by-related-posts

You should change the code accordingly with your other remaining files.

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