Skip Navigation

[Resolved] Display Data from Relationship in JSON Output of a View

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

Problem:

Setup a custom field value of child post to parent post ID, both in admin side and front-end with Toolset form.

Solution:

1) In wordpress admin side, you can try with action hook save_post, for example:

https://toolset.com/forums/topic/display-data-from-relationship-in-json-output-of-a-view/#post-1146343

2) In front-end with Toolset form, you can try with action hook cred_save_data, for example:

https://toolset.com/forums/topic/display-data-from-relationship-in-json-output-of-a-view/#post-1146372

Relevant Documentation:

This support ticket is created 6 years, 2 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 9 replies, has 3 voices.

Last updated by bjoernH 6 years, 1 month ago.

Assisted by: Luo Yang.

Author
Posts
#1138851

I need to output data from a view for a RESTful API in JSON format. This works fine using the plugins "Views Output Formats" or Adrianos "Toolset Rest". Unfortunately, since the relationship of CPTs is now saved differently, I do not get the data from related posts anymore.
Can you give me a hint how I could also get data from related posts to my JSON queries?

#1138891

Nowadays you can output raw Views Loop data and format it as you like since it will be raw.
When you head to a View's Loop you can see that a 7th option was added to the Wizard:
List with separators

Then, below the editor there is a checkbox:
Disable the wrapping DIV around the View

Those 2 features together will allow you to output practically any data format you want.
Then, you can either place that View with the ShortCode anywhere you want or call it with PHP:
https://toolset.com/documentation/programmer-reference/views-api/

Now, REST is something else that Toolset does not yet fully support.
You can enable REST for Post Types when you edit a Post Type or create one on Toolset > Post Types.
However, Fields, terms and others do not support it and Adriano's (outdated) plugin is the only resource I know of.

I suggest you add your voice to our most requested feature here:
https://toolset.com/feature-request/?wpv_post_search=REST&wpv-wpcf-plugins=Types&wpv_view_count=686737

#1145056

Beda, I am still struggling with finding a workaround for this.
I have added a custom field to my child-custom post type which should automatically be field with the parentID whenever a post is stored or updated.

This works for updating, but for new posts it does not. I guess the problem is, that the "postID" is not available when I save the post. Unfortunately, your answer here (https://toolset.com/forums/topic/using-parent-fields-in-a-cred-form-as-field-values/) clarifies, that a workaround in the CRED form will also not help.

This is my code:

function save_siteId( $post_ID ) {

if ( get_post_type( $post_ID ) == 'truck' && isset( $GLOBALS['post'] ) ) {

// Get Site from RelatedItem
$truck_id = get_post( get_the_ID() ); // Get ID of current truck
$site_id = toolset_get_related_post( $truck_id, 'site-truck', 'parent' );

update_post_meta( $post_ID, 'wpcf-trucksite', $site_id );
}

}
add_action( 'save_post', 'save_siteId', 20, 2 );

As said, this works for editing and the field "wpcf-trucksite" is filled. But for new posts, I only get a "0" as value. Any tips?

#1145576

Hello,

I assume we are talking about this case:

- Post types "site" and "truck" with one-to-many relationship, if you are using old parent/child post type relationship, you can follow our document to migrate the post type relationships
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/

- you are going to output all "site" posts information into JSON format data.

If it is, you can try these:
1) Create a post view, query posts of "site" posts,
2) enable the option "Disable the wrapping DIV around the View"
3) In the section "Loop Editor", output the JSON format data, like this:

...
<wpv-loop>
[wpv-item index=1]{"value":"[wpv-post-id]","label":"[post-title]"}
[wpv-item index=other],{"value":"[wpv-post-id]","label":"[post-title]"}
</wpv-loop>
...

This should be able to output the JSON format data.

#1145739

Thanks, Luo, this works fine for me now.
The additional problem with filling the field "wpcf-trucksite" with the parentID remains, though. I have found this ticket, which seems similar to my problem:

https://toolset.com/forums/topic/cannot-get-parent-post-id-on-cred_save_data-with-new-relationships-api/#post-1145738

Thats my problem in detail:

Parent Post Type "Site -> Child Post Type "Truck"

---
function save_siteId( $post_ID ) {

if ( get_post_type( $post_ID ) == 'truck' && isset( $GLOBALS['post'] ) ) {

// Get Site from RelatedItem

$truck_id = get_post( get_the_ID() ); // Get ID of current truck

$site_id = toolset_get_related_post( $truck_id, 'site-truck', 'parent' );

// return $site_id;

update_post_meta( $post_ID, 'wpcf-trucksite', $site_id );
}
}
add_action( 'save_post', 'save_siteId', 20, 2 );

----
This function works, but only if the post is updated. For a new post, this does not work, it only writes "0" in my field "wpcf-trucksite". Not sure why this is so.

#1146306

You are using wordpress built-in action hook save_post, this is a custom PHP codes problem, please provide a test site with the same problem, also point out the problem page URL and where I can edit your PHP codes, I need a live website to test and debug. thanks

#1146350

Thanks for the details, I have modified your PHP codes as below:

   function save_siteId( $truck_id ) {
      
    if ( get_post_type( $truck_id ) == 'truck' ) {
        // Get Site from RelatedItem
		
		$site_id = toolset_get_related_post( $truck_id, 'site-truck', 'parent' );
	
	  //  return $site_id;
		
	  update_post_meta( $truck_id, 'wpcf-trucksite', $site_id );
	  
     }
      
}

Please test again, check if it is fixed or not, thanks

#1146367

Hi Luo,

thanks for your help here, the code looks better now.
It works from the Admin panel, but if I use the toolset form (hidden link) to add a new truck, the wpcf-trucksite is still set to "0". I suppose this is due to the fact that the truckId is not set, when the post is saved.

Would it be possible to use a different hook when the post is saved other than "post_saved"?

#1146372

Yes, you are right, you can try Toolset form API action hook "cred_save_data", I have modified the codes in "CRED Submit Form -postID", as below:

add_action('cred_save_data', 'my_save_action', 999, 2);
function my_save_action($truck_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==2843)
    {
	  $site_id = 0;
	  if(isset($_POST['@site-truck_parent'])){
	  	$site_id = $_POST['@site-truck_parent'];
	  }
	  update_post_meta( $truck_id, 'wpcf-trucksite', $site_id );
    }
}

Please test again, check if it is fixed both in admin side and front-end

#1146381

My issue is resolved now. Thank you!