Skip Navigation

[Resolved] Is there any way to write data to a post’s field without using a form?

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

Problem:

I've had several instances where I've wanted to write a piece of data directly to a post, but not in response to filling out a post form. Rather, during the execution of a script. Is this possible?

Solution:

There isn't such kind of built-in feature within Views, Views is for displaying data, it can not update data.

You might consider custom codes, for example:

https://toolset.com/forums/topic/is-there-any-way-to-write-data-to-a-posts-field-without-using-a-form/#post-1506487

Relevant Documentation:

https://codex.wordpress.org/Function_Reference/add_shortcode

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

Last updated by ericE-4 4 years, 10 months ago.

Assisted by: Luo Yang.

Author
Posts
#1506163

I've had several instances where I've wanted to write a piece of data directly to a post, but not in response to filling out a post form. Rather, during the execution of a script. Is this possible?

#1506293

Hello,

I assume you are going to use other application/software to update your WordPress post, you might consider WordPress REST api, for example:
https://developer.wordpress.org/rest-api/reference/posts/#update-a-post
Update a Post

In Toolset sider, you can enable the REST api for custom post types, for example:
Dashboard-> Toolset-> Post Types, find and edit your custom post type, enable option "show_in_rest".

More help:
https://codex.wordpress.org/Function_Reference/register_post_type#show_in_rest

#1506481

No I'm not using other software to update a post. Just WordPress. If a post is being shown with a view, I'd like to be able to write data to the post programmatically via JS or PHP in response to a user interaction or even automatically.

#1506487

There isn't such kind of built-in feature within Views, Views is for displaying data, it can not update data.

You might consider custom codes, for example:
Create a shortcode:

add_shortcode('update_meta_value', function($atts, $content){
	$post_id = get_the_ID();
	$v = get_post_meta($post_id, 'wpcf-my-field', true);
	$v += 1;
	update_meta_value($post_id, 'wpcf-my-field', $v);
});

Then display above shortcode [update_meta_value] insider view's loop.

It will be able to update custom field value "my-field" +1.

More help:
https://codex.wordpress.org/Function_Reference/add_shortcode

#1506505

OK, so it is possible. I will play around with this code snippet.

My issue is resolved now. Thank you!