Skip Navigation

[Resolved] Sort by date post relationship AGAIN

This support ticket is created 6 years, 6 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 6 replies, has 2 voices.

Last updated by olivierF 6 years, 6 months ago.

Assisted by: Nigel.

Author
Posts
#918742

Hi (Nigel ?)
The problem on this page returned but I do not know why, the page : hidden link
the old post with the explanation of the problem :
https://toolset.com/forums/topic/sort-by-date-post-relationship/
Thank you
Olivier

#919174

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Olivier

I saw that you were dealing with the translation issue with Beda and for debugging were disabling the custom code.

Is the code I provided before still being applied?

What other changes have you made?

#919201

Hi Nigel,

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 );            
        }
    }
}
#919306

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Olivier

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.

#919609

Hi Nigel,

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?

Thanks.

#919960

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

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:

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);

That will create a file debug.log in your wp-content/ directory which you can examine in any text editor.

#922378

Hi Nigel.
updating to type 3 seems to have solved the problem.
Thanks a lot for your help.
Olivier