Support Forum
Support › MB REST API › Not able to post through REST API
Hi,
since July I was not able to update metabox fields through the REST API with this error code:
8-Jul-2024 00:45:10 UTC] PHP Fatal error: Uncaught Error: Call to undefined method WP_Post::get_id() in /home/hannl/public_html/wp-content/plugins/mb-rest-api/src/Post.php:9
After changing the code in POST.php in line 9 from:
$post_id = $post->get_id();
to:
class Post extends Base {
public function update( $data, $post ) {
$post_id = $post->ID;
$this->update_values( $data, $post_id, $post->post_type );
}
it is working again. Could you please take a look into it and clarify this?
Thank you
Hello Michael,
The code that you removed is used to update the WooCommerce product data. Do you have the custom post type with the same slug "product"?
if ( property_exists( $post, 'post_type' ) && 'product' === $post->post_type ) {
$post_id = $post->get_id();
}
Hi Peter,
No, I just have woo commerce products, nothing else. I’m importing/updating data from a database with a python script to the site, it was working fine, after updating to 2.02 it was not possible to update the products anymore and the error message suggested an error in line 9.
What I do:
Getting data and creating a woo simple product through Woo API.
Creating a post in custom post type "autor" with WordPress API.
Creating a post in custom post type "translator"with WordPress API..
Updating metabox fields and relations through WordPress API.
Running a cronjob and updating new versions of the connected database.
After removing the code it is running smoothly again.
I was wondering, as you were updating to make the product update possible, but here it was the opposite result.
Hello,
Thank you for sharing the details.
Can you try the steps one by one and let me know which causes the issue?
Getting data and creating a woo simple product through Woo API.
Creating a post in custom post type "autor" with WordPress API.
Creating a post in custom post type "translator"with WordPress API..
Updating metabox fields and relations through WordPress API.
Then share some code or screenshots of the steps you do so I can reproduce the issue on my end.
Here is the error log:
[18-Jul-2024 00:52:08 UTC] PHP Fatal error: Uncaught Error: Call to undefined method WP_Post::get_id() in /home/hannl/public_html/wp-content/plugins/mb-rest-api/src/Post.php:9
Stack trace:
#0 /home/hannl/public_html/wp-includes/rest-api/endpoints/class-wp-rest-controller.php(474): MetaBox\RestApi\Post->update()
#1 /home/hannl/public_html/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php(939): WP_REST_Controller->update_additional_fields_for_object()
#2 /home/hannl/public_html/wp-includes/rest-api/class-wp-rest-server.php(1230): WP_REST_Posts_Controller->update_item()
#3 /home/hannl/public_html/wp-includes/rest-api/class-wp-rest-server.php(1063): WP_REST_Server->respond_to_request()
#4 /home/hannl/public_html/wp-includes/rest-api/class-wp-rest-server.php(439): WP_REST_Server->dispatch()
#5 /home/hannl/public_html/wp-includes/rest-api.php(420): WP_REST_Server->serve_request()
#6 /home/hannl/public_html/wp-includes/class-wp-hook.php(324): rest_api_loaded()
#7 /home/hannl/public_html/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters()
#8 /home/hannl/public_html/wp-includes/plugin.php(565): WP_Hook->do_action()
#9 /home/hannl/public_html/wp-includes/class-wp.php(418): do_action_ref_array()
#10 /home/hannl/public_html/wp-includes/class-wp.php(813): WP->parse_request()
#11 /home/hannl/public_html/wp-includes/functions.php(1336): WP->main()
#12 /home/hannl/public_html/wp-blog-header.php(16): wp()
#13 /home/hannl/public_html/index.php(17): require('...')
#14 {main}
thrown in /home/hannl/public_html/wp-content/plugins/mb-rest-api/src/Post.php on line 9
Hello,
What steps throw the error on your site? Is this the last step?
Updating metabox fields and relations through WordPress API.
If yes, can you share some screenshots or code that you use to update the meta box fields? And let me know the post type which has the custom field that you are trying to update.
The error is thrown when I try to update the metabox-data with wp2 API for a product:
https://hannl.bw-works.com/wp-json/wp/v2/product/16441
{
"code": "internal_server_error",
"message": "<p>Es gab einen kritischen Fehler auf deiner Website.<\/p><p><a href="https:\/\/wordpress.org\/documentation\/article\/faq-troubleshooting\/">Erfahre mehr über die Problembehandlung in WordPress.<\/a><\/p>",
"data": {
"status": 500,
"error": {
"type": 1,
"message": "Uncaught Error: Call to undefined method WP_Post::get_id() in \/home\/hannl\/public_html\/wp-content\/plugins\/mb-rest-api\/src\/Post.php:9\nStack trace:\n#0 \/home\/hannl\/public_html\/wp-includes\/rest-api\/endpoints\/class-wp-rest-controller.php(474): MetaBox\\RestApi\\Post->update()\n#1 \/home\/hannl\/public_html\/wp-includes\/rest-api\/endpoints\/class-wp-rest-posts-controller.php(939): WP_REST_Controller->update_additional_fields_for_object()\n#2 \/home\/hannl\/public_html\/wp-includes\/rest-api\/class-wp-rest-server.php(1230): WP_REST_Posts_Controller->update_item()\n#3 \/home\/hannl\/public_html\/wp-includes\/rest-api\/class-wp-rest-server.php(1063): WP_REST_Server->respond_to_request()\n#4 \/home\/hannl\/public_html\/wp-includes\/rest-api\/class-wp-rest-server.php(439): WP_REST_Server->dispatch()\n#5 \/home\/hannl\/public_html\/wp-includes\/rest-api.php(420): WP_REST_Server->serve_request()\n#6 \/home\/hannl\/public_html\/wp-includes\/class-wp-hook.php(324): rest_api_loaded()\n#7 \/home\/hannl\/public_html\/wp-includes\/class-wp-hook.php(348): WP_Hook->apply_filters()\n#8 \/home\/hannl\/public_html\/wp-includes\/plugin.php(565): WP_Hook->do_action()\n#9 \/home\/hannl\/public_html\/wp-includes\/class-wp.php(418): do_action_ref_array()\n#10 \/home\/hannl\/public_html\/wp-includes\/class-wp.php(813): WP->parse_request()\n#11 \/home\/hannl\/public_html\/wp-includes\/functions.php(1336): WP->main()\n#12 \/home\/hannl\/public_html\/wp-blog-header.php(16): wp()\n#13 \/home\/hannl\/public_html\/index.php(17): require('...')\n#14 {main}\n thrown",
"file": "\/home\/hannl\/public_html\/wp-content\/plugins\/mb-rest-api\/src\/Post.php",
"line": 9
}
},
"additional_errors": []
}
Hello,
To update the WooCommerce product, you need to use its Rest API endpoint v3, with the field ID and value in the body request.
{
"meta_data": [
{
"key": "field_id",
"value": "field value"
}
]
}
For example: I use Postman app to update a field value of a product ID 2086
URL http://site.com/metabox/wp-json/wc/v3/products/2086
screenshot https://imgur.com/nUMwFkJ
Refer to this topic
https://stackoverflow.com/questions/70696204/update-order-custom-field-value-with-woocommerce-rest-api
Has there been a change?
Last time you told me to update the matabox-data through REST API v2, so I’m a bit confused now:
https://support.metabox.io/topic/getting-fatal-error-on-psoting-updating-posts-via-rest-api/#post-43867
The workflow is:
Creating a product with WC API v3
Updating metabox custom fields with WP API v2
This was working fine unless the code in your metabox-extension changed.
After changing the code ($post_id = $post->ID;) there as described it is again working fine.
I’m just curious if I have to change the code after every update …
I understand your situation. However, to adapt to new changes from WooCommerce, we also need to update the code on our side. Please bear with us.
Let me know if you can update the field value via WC API v3.
I tried to update via WC API v3 like this, did not work:
{
"meta_box": {
"relatedlist": [],
"untertitel": "Der ausagekräftige Untertitel",
"format": "",
"einband": "Softcover",
"illus": "",
"umfang": "456",
}
}
If I update like this, it does work:
{
"meta_data": [
{
"key": "umfang",
"value": "456"
}
]
}
This is still with the modified code of your plugIn.
And it I would need to change the whole import script to adjust to that.
With the unchanged code from mb-rest-API plugin it is also working:
{
"meta_data": [
{
"key": "preis_at",
"value": "29,90"
}
]
}
result:
"meta_box": {
"preis_at": "29,90"
}
So I really have to change the import structure again?
I did this already last time, when you advised to updates meta-box CF via WP API v2 …
Yes, at least for now. I've informed the development team to check if we can support working with WooCommerce products via MB Rest API v2. Otherwise, we will update the documentation regarding WooCommerce product because it has its own API.
Thank you.
Ok, so I’ll change everything to the WC APIv3 now. Can you please confirm that from your side this is future proof?
Thank you
Next problem: I have a relationship field which is not recognized by the WC APIv3.
And I cannot update that via WP APIv2 as I get a 500 error.
So what to do now?
thx