Tell us what you are trying to do?
I want to populate a types field with the post id
[code]
function save_fact_meta( $post_id, $post ) {
$post_type = get_post_type($post_id);
// If this isn't a 'fact' post, don't update it.
if ( "faktarutor" != $post_type ) return;
update_post_meta( $post_id, 'wpcf-post-id', $post_id );
add_post_meta( $post_id, 'wpcf-post-id', $post_id );
}
add_action( 'save_post', 'save_fact_meta', 20,2 );
[/code]
It only works inside the custom post 'edit post' screen and not the relationship tab (see screenshots)
Hi, this is a tricky problem to solve. It happens because the custom field values are actually saved again after the save_post event. Typically you would leave that field empty when you create the new post, so a empty value is saved over the post ID you set with code. You can work around the problem by adding another event hook triggered when the post relationship is created. This fixes the problem where the post ID is not saved when the post is created. Someone can change the post ID field value later by clicking "Quick Edit" in the post relationship panel, or by editing the post directly in wp-admin. Here is a code template you can use:
// automatically set the post ID field when creating a fakturator post in the post relationship panel of another post
add_action( 'toolset_association_created', 'tssupp_automate_post_id_field_in_pr', 10, 5 );
function tssupp_automate_post_id_field_in_pr( $association_slug, $parent_id, $child_id, $intermediary_id, $association_id ) {
// edit these slugs if necessary
$rel_slug = "post-fakturator";
$field_slug = "post-id";
$child_type_slug = "fakturator";
// you should not edit below this line
if ( $rel_slug === $association_slug ) {
add_filter('wpcf_fields_postmeta_value_save' , function( $value, $type, $slug, $cf, $obj ) use ( $child_id, $field_slug, $child_type_slug ) {
if ( $field_slug === $slug && get_post_type($child_id) == $child_type_slug ) {
return $child_id;
}
}, 10, 5 );
}
}
You must modify the $rel_slug value if necessary. It should contain the post relationship slug for this post relationship. In Toolset > Relationships, you can click "Edit Relationship" to find the relationship slug.
If you would prefer that no one can ever change the custom field value, I can show you how to use the wpcf_fields_postmeta_value_save hook directly without using the toolset_association_created hook. However, this prevents anyone from changing the post ID field value later. You would have to manually modify the value in the database. There is a minor display issue when you implement it this way. If someone tries to change the post ID field value by clicking Quick Edit in the Post Relationship panel, they can submit the change and it looks like the value was saved. However, if you refresh the page, you can see that the post ID field was not actually changed. Let me know if you want more information about that.
Hi Chris
Thank you so much for the code.
I will allow someone to change it or maybe I will just make some javascript to prevent it and make it readonly.
document.getElementById("-textfield-1-1570607690").setAttribute("readonly", true);
Does the id change on each time the admin loads?
The population code does not seem to work... did I write the wrong slugs?
I followed your instructions and found the '$rel_slug'. The 'field_slug' I typed in the types field and '$child_type_slug'
// automatically set the post ID field when creating a fakturator post in the post relationship panel of another post
add_action( 'toolset_association_created', 'tssupp_automate_post_id_field_in_pr', 10, 5 );
function tssupp_automate_post_id_field_in_pr( $association_slug, $parent_id, $child_id, $intermediary_id, $association_id ) {
// edit these slugs if necessary
$rel_slug = "post-faktaruta";
$field_slug = "wpcf-post-id";
$child_type_slug = "faktarutor";
// you should not edit below this line
if ( $rel_slug === $association_slug ) {
add_filter('wpcf_fields_postmeta_value_save' , function( $value, $type, $slug, $cf, $obj ) use ( $child_id, $field_slug, $child_type_slug ) {
if ( $field_slug === $slug && get_post_type($child_id) == $child_type_slug ) {
return $child_id;
}
}, 10, 5 );
}
}
Also why does the post body not show up in the relationship meta-box?
Does the id change on each time the admin loads?
That's right, the UI creates unique IDs each time the page loads so you cannot rely on a consistent ID here. If you want to write your own custom JS you could use an attribute selector to test the "name" attribute, similar to this:
jQuery("input[name='wpcf[post][post-id]']")....
More info about that selector: https://api.jquery.com/attribute-equals-selector/
The population code does not seem to work... did I write the wrong slugs?
I see at least one issue. You should remove the "wpcf-" prefix from the field slug here:
$field_slug = "wpcf-post-id";
Other than that, it looks okay but I would have to check wp-admin to be sure.
Hi Chris
Now is works on newly created 'faktaruta' but not on connecting to existing.
Can the function update the field when clicked on the QuickEdit update button?
Now is works on newly created 'faktaruta' but not on connecting to existing.
But Faktaruta posts created outside of the post relationships panel will already have the post ID field saved by the save_posts hook, right? Or please explain how the post ID field would not already be set for the existing child post.
Can the function update the field when clicked on the QuickEdit update button?
No, the toolset_association_created hook is only fired when the parent and child posts are linked here. I don't understand why it would be necessary though. When the child post is created it is either created outside of the Post Relationship panel (and the save_post hook was fired), or it is created inside the Post Relationship panel ( and the toolset_association_created hook was fired). Either way, the post ID field should already be set. Or am I misunderstanding something?
Hi Chris
Thank you so much for all the help. I will use the function as is.
Can I get the post body field to show up in the relationship tab?
No, this listing is similar to a post type listing in wp-admin. Only the title and simple custom fields will appear here, no WYSIWYG content.