Skip Navigation

[Resolved] Uupdate custom field on pr_save_child_post

This support ticket is created 3 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.

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/Karachi (GMT+05:00)

This topic contains 19 replies, has 3 voices.

Last updated by Waqar 3 years, 10 months ago.

Assisted by: Waqar.

Author
Posts
#1922629

Hello,

on the save action on Post RelationShip metabox, i would like to automatically fill a field with a value of 2 concatenating fields and get this value shows up on after the Ajax Refresh.
example:
Data: Custom Field 1 (having a value), Custom Field 2(having a value) , Custom Field 3( empty and readonly).
by hitting save ==> Custom Field 3 will be populated by Custom Field1 + Custom Field 2

i have tried the following codes " a minimal one to explain the simple result" but the value is not inserted on the Database

add_action('post_updated', 'add_new_match', 10, 2);
function add_new_match($post_id,$post_data) {
/*add_post_meta also tested */ update_post_meta($post_id, 'wpcf-my-custom-field','new Value',false);
}

or : add_action( 'pr_save_child_post', 'add_new_match', 99, 4 );
function add_new_match($post_id,$post_data) {
/*add_post_meta also tested */ update_post_meta($post_id, 'wpcf-my-custom-field','new Value',false);
}

or : add_action( 'updated_post_meta', 'add_new_match', 99, 4 );

By a debug and some dummy data i can get it updated if i'm hitting save on a different record with a specific $post_id passed on the update_post_meta function :
I hit save on record id 3046 and passing the post_id value 3045

add_action('post_updated', 'add_new_match', 10, 2);
function add_new_match($post_id,$post_data) {
/*add_post_metaalso */ update_post_meta(3045, 'wpcf-my-custom-field', 'new Value', false);
}

the save button have this function: mysite.com/admin-ajax.php?action=wpcf_ajax&wpcf_action=pr_save_child_post&post_type_parent=my-post-type&post_id=3047&parent_id=2899&post_type_child=child-post&_wpnonce=4b422bd202

if you can please help to get it through.

thank you

#1922723

Hello,

The action hook "post_updated" works only for editing existed post.

For the Toolset Post RelationShip metabox, please try the action hook "toolset_association_created", see our document:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_association_created

This action is executed just after creating a new association between two posts.

#1924561
SaveChildRecord.png

i have tried "toolset_association_created", on save post child record/Add new record nothing was triggered:

add_action( 'toolset_association_created', 'handle_association_created', 10, 5 );

function handle_association_created( $association_slug, $parent_id, $child_id, $intermediary_id, $association_id ) {
error_log( 'MY STUFF: ' . print_r( $parent_id, true ) );
}

could you please help?

#1927263

It is a custom PHP codes, please provide a test site with the same problem, fill below private message box with login details and FTP access, also point out the problem page URL, where I can edit your custom PHP codes.

Private message box enabled.

#1929193

I can login your website, here are what I found.

You are using a very old version of Toolset Types plugin version 2.3.5, but the latest version is 3.4.6.

I suggest you try these:
1) upgrade Toolset Types plugin of your website to the latest version, you can download it here:
https://toolset.com/account/downloads/

2) Follow our document to setup one-to-many relationship between "Meetings" and "Meeting Records"
https://toolset.com/course-lesson/how-to-set-up-post-relationships-in-wordpress/

3) Then use WP action hook "save_post" to update the custom post meta value, for example:

add_action('save_post', function($post_id, $post, $update){
	if($post->post_type == 'meeting-record'){
		$year = get_post_meta($post_id, 'wpcf-year', true);
		$order = get_post_meta($post_id, 'wpcf-order', true);
		$year_order = '';
		if($year && $order){
			$year_order = $year . '-' . $order;
		}
		if($year_order){
			update_post_meta($post_id, 'wpcf-year-order', $year_order);
		}
	}
},999,3);

Notice, above codes works only after you save the child post and refresh the parent "Meetings" post

#1934321

Thank you Luo for your reply and support.

We were using the 2.3.5 version for couple of years and our end users have been used to this view.
Some changes that we notice:
1- "Add a new record" button open a popup instead of a row on the same table. if we cannot have the the old way/behavior we are looking to get 2 fields aligned on the same row. i tried to apply some CSS Styling but the way the popup is made doesn't help to get all fields aligned correctly.

2-"Connecting Existing meeting Record" will not be useful and we will need to hide it since end users will get access to this backend page and we don't want them to get confused.

3- The solution you have shared actually it did work but only when we hit the Edit button, the record is opened in a new tab as a post. Using "Quick Edit" it doesn't work, the Year Order didn't populate.

if you can please help on the 3 points highlighted.

thank you

#1934327

regarding the third point, also "add new record" does not populate the year order. if you can also help on it.
if there a away to make the YearOrder filed a read only one ?

thank you

#1934543

For the new questions:
Q1) "Add a new record" button open a popup instead of a row on the same table. if we cannot have the the old way/behavior we are looking to get 2 fields aligned on the same row. i tried to apply some CSS Styling but the way the popup is made doesn't help to get all fields aligned correctly.
There isn't such kind of built-in feature within Toolset Types plugin 2.3.5, but it is possible with latest version Toolset Types plugin

Q2) "Connecting Existing meeting Record" will not be useful and we will need to hide it since end users will get access to this backend page and we don't want them to get confused.
There isn't such kind of feature with Types plugin, even the latest version, you will consider custom codes to hide them.

Q3) We recommend you to use the latest version Toolset plugins to avoid other unexpected results, for example, the old version of Types plugin might not compatible with future version of WordPress.
Please check my answer above:
https://toolset.com/forums/topic/uupdate-custom-field-on-pr_save_child_post/#post-1929193

#1934721
CaptureQuickEdit.PNG

Hi Luo,

i think there is a misunderstanding here.
i upgraded the Toolset from 2.3.5 to 3.4.6. we are trying to acheive the 3 highlighted points shared before with your help.

Q1- we need to restyle the popup fields to get them aligned on the same row( 2fields on one line/row)
Q2- could you please share this custom code to hide this button "Connecting Existing meeting Record"?
Q3- the code you shared before is not working on the Quick Edit menu (Please see the attached image), when hitting save the YearOrder Field does not get populated, this code is only working when we are on the edit page of the record(Child Post). Could you please help on that since it's the most important functionality needed.

Thank you

#1938945

Hi Rene,

Luo is on vacation, so I'll be following up on this ticket.

I'm reviewing your questions and will reply to them point-by-point shortly.

Thank you for your patience.

regards,
Waqar

#1940747

thank you, will wait for your reply .

#1941039

Thank you for waiting while I performed some testing and research.

Q1- we need to restyle the popup fields to get them aligned on the same row( 2fields on one line/row)
Q2- could you please share this custom code to hide this button "Connecting Existing meeting Record"?

> For these two points, you can load some custom CSS code in your website's admin area, for example:


add_action('admin_head', 'my_custom_CSS');

function my_custom_CSS() {
ob_start();
?>
	
<style>
.types-quick-edit-fields-post .types-fields-container > .wpt-field {
    width: 48%;
    float: left;
    margin-right: 4%;
}

.types-quick-edit-fields-post .types-fields-container > .wpt-field:nth-child(even) {
    margin-right: 0%;
    clear: right;
}

.types-related-content-actions input[value="Connect existing Meeting Record"] {
    display: none;
}
</style>

<?php
echo ob_get_clean();
}

Q3- the code you shared before is not working on the Quick Edit menu (Please see the attached image), when hitting save the YearOrder Field does not get populated, this code is only working when we are on the edit page of the record(Child Post). Could you please help on that since it's the most important functionality needed.

> I've tested a couple of different WordPress and Toolset hooks to programmatically update the custom field values, through the "Quick Edit" menu in the relationship meta box.

The challenge here is that even if you manage to update them instantaneously, the original value is overwritten once the regular AJAX update completes. Unfortunately, I can't think of any other hooks or methods to make this work, but there are a couple of workarounds that you can use:

1. Using the custom CSS code, you can hide the "Quick Edit" links in the relationship meta box, so that the users can only use the edit link to go to the respective post's edit screen to change the field values.

OR

2. Instead of allowing admin area access to post edit screens, you can create front-end Toolset Forms for your users, so that they can do the editing only from the front-end.

This will mean some extra work, but, controlling the design/layout and data manipulation like this would be much more flexible on the front-end, compared to the admin area.

#1950447

Hi Waqar,

sorry for taking time to reply to your last answer.
actually the Q1-Q2 answer was not for the popup when we click on "add new record button" the objective is to align two field on the same row, if you can help on that.

for the Q3 it's not practical to send users each time to the edit post page for any change, the quick edit is the best approach. do you believe that only way to achieve it is to redirect user to a front end page?

thank you

#1951555

Thanks for writing back.

Sorry about the confusion and here is the updated custom CSS code that should make the fields in the Add new item popup, two-column:


add_action('admin_head', 'my_custom_CSS');
 
function my_custom_CSS() {
ob_start();
?>
     
<style>

#relationship-field-input-container-new > .wpt-field,
.types-quick-edit-fields-post .types-fields-container > .wpt-field {
    width: 48%;
    float: left;
    margin-right: 4%;
}

#relationship-field-input-container-new > .wpt-field:nth-child(even),
.types-quick-edit-fields-post .types-fields-container > .wpt-field:nth-child(even) {
    margin-right: 0%;
    clear: right;
}
 
.types-related-content-actions input[value="Connect existing Meeting Record"] {
    display: none;
}
</style>
 
<?php
echo ob_get_clean();
}

For the Q3, I'm afraid, I'm out of ideas, and to make this automatic filling work with the quick edit functionality, you'll need some fairly complex PHP or JS script customization. For more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

#1954987

thank you for your reply.

i have tested the my_custom_CSS and still need some adjustment....i wil get back to it later. For the Q3 i will not try to get it automated for now...

i have 2 different concerns:

1st: i need to set "Title" column/field to be displayed as the second column on the relationship table (applied some code but i cannot control columns on the relationship table) . i assume that Title is created by default (Post Title); if there is a way to hide it and create a custom field "Title" instead; or to make this post title the 2nd column( Same need to be reflected on New Record Popup field)

2nd: need to get the Quick Edit enabled by default without having to click on the "Enable button" for each new created record,( i know that there is the check box to not be displayed , but for an end user perspective it not so user friendly)

thank you for your support!