[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.
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.
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():
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);
}
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.