I can not say since when it does not work anymore. The code below is still present in the file "functions.php" I reactivated the plugin snippets which no longer poses a problem. I run once the code but that did not do anything.
When I encode a new concert, the fields do not appear anymore. For example on this page. hidden link
add_action( 'save_post', 'tssupp_duplicate_parent_field', 101, 3 );
function tssupp_duplicate_parent_field( $post_id, $post, $update ){
$child_slug = 'participant';
$parent_slug = 'concert';
$parent_field_slug = 'concert-date';
// is it a child post being updated?
if ( $child_slug == $post->post_type ) {
// get the ID of the parent post
$parent_id = get_post_meta( $post_id, '_wpcf_belongs_' . $parent_slug . '_id', true );
if ( !empty( $parent_id ) ) {
// get the parent field
$parent_field_value = get_post_meta( $parent_id, 'wpcf-' . $parent_field_slug, true );
// duplicate the parent field on the child post
update_post_meta( $post_id, 'wpcf-' . $parent_field_slug, $parent_field_value );
}
}
}
This was set up before Types 3 and the new relationships, yes?
Have you since updated and run the relationships migration?
We have a backwards compatibility layer to ensure such things should still work, but maybe it is not in this case.
I've added a line to output the parent post id when you save a child post to check whether the code should still be working:
add_action( 'save_post', 'tssupp_duplicate_parent_field', 101, 3 );
function tssupp_duplicate_parent_field( $post_id, $post, $update ){
$child_slug = 'participant';
$parent_slug = 'concert';
$parent_field_slug = 'concert-date';
// is it a child post being updated?
if ( $child_slug == $post->post_type ) {
// get the ID of the parent post
$parent_id = get_post_meta( $post_id, '_wpcf_belongs_' . $parent_slug . '_id', true );
error_log("parent id is " . $parent_id);
if ( !empty( $parent_id ) ) {
// get the parent field
$parent_field_value = get_post_meta( $parent_id, 'wpcf-' . $parent_field_slug, true );
// duplicate the parent field on the child post
update_post_meta( $post_id, 'wpcf-' . $parent_field_slug, $parent_field_value );
}
}
}
Can you update the code, make sure your debug.log is turned on, save a child post (for a new entry) and then check the log to see if it was able to identify the parent?
If not it looks like the code will need to be modified to work with the new relationships API.
Yes, this was set up before Types 3 and the new relationships.
I have not done the last update to type 3, can I upgrade without losing my job ?
How can I check that debug.log is turned on and check the log?
You will need to update to Types 3 at some point, but you don't need to run the migration wizard that updates the post relationships, the old-style relationships will continue to work as-is if you don't make any changes.
I recommend—aside from this issue—that you upgrade to Types 3 on a staging site and confirm that everything still works as expected.
Meanwhile, for the debug log,
If you haven't already, turn on the debug log by editing your wp-config.php file and change the line with WP_DEBUG like so: