Skip Navigation

[Resolved] wpcf_pr_post_get_belongs() does not return parent id

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

Problem:
wpcf_pr_post_get_belongs() does not return parent id

Solution:
Since Types 3.0 which is published with new post relationship API, you need to use the post relationship API function "toolset_get_related_post" to get the pareant ID.

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

This support ticket is created 5 years, 11 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 3 replies, has 2 voices.

Last updated by Monika Beck 5 years, 11 months ago.

Assisted by: Minesh.

Author
Posts
#1169015

I am trying to auto populate the excerpt field of my toolset post type (product-document) with the title of the parent related post (products-all). Basically what I need is product-documents to be searchable by the title of the related parent post.
Here is the code what I have been trying, but it does not work:
function myplugin_save_postdata($post_id,$post) {
global $wpdb;
if($post->post_type == 'product-document'){
$date = date('n/d/Y');
$mydata1 = get_the_title( $post_id );
$file = get_post_meta($post_id,'wpcf-file',true);
$mydata2 = basename($file);
$title = $mydata1 . ' (' . $mydata2 . ') ' . $date;
$parent_id = wpcf_pr_post_get_belongs($post_id, 'products-all');
$myExcerpt = get_the_title($parent_id);
//die($myExcerpt);
remove_action('save_post', 'myplugin_save_postdata',30,2);
wp_update_post( array(
'ID' => $post_id,
'post_excerpt' => $title . ',' . $myExcerpt,
));
add_action('save_post', 'myplugin_save_postdata',30,2);
}
}

wpcf_pr_post_get_belongs() does not return the parent id.

#1169452

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

I see you are using Types version 3.2.1, Since Types 3.X.X we implemented the new post-relationship and to support that we offer new post relationship API:

If you start your project using Types >= 3.X.X, you should use our new post-relationship API, as I understand you want to fetch the parent ID, so you should use the function toolset_get_related_post():

More info:
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

#1169882

I have tried that as well and it returns nothing... Here is the code I have used to test. die($parent_post->post_title); is empty...

$parent_posts = toolset_get_related_posts(
    $post_id, // get posts related to this one
    'products-all', // relationship between the posts
    'child', // $result->ID is parent in given relationship
    100, // pagination
    0,
    array(), // meta query filters?
    'post_object', // return type
    'parent' //posts from the relationship should be returned
);

foreach ($parent_posts as $parent_post) {
	die($parent_post->post_title);
}
#1170291

Minesh
Supporter

Languages: English (English )

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

Well - Could you please share problem URL and where you added the code you share as well what data you are trying to fetch from which relationship?

*** 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 would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

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