Skip Navigation

[Resolved] Update parent field after checking others child fields

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

Problem:

parent post type: "contatto" with types custom field "data-inizio-ultimo-corso"
child post type: "corso" with types custom field "data-inizio-corso"
i'm using view filtering only "contatto" post types (because we don't want to see duplicates)

when submitting a new "corso", i'm updating "data-inizio-ultimo -corso" if date greater, using this function:

//DATA INIZIO ULTIMO CORSO
function inserisce_data_inizio_ultimo_corso( $post_ID ) {     
    if ( get_post_type( $post_ID ) == 'corso' ) {
                $data_inizio_corso_inserito_timestamp = get_post_meta($post_ID, 'wpcf-data-inizio-corso', true);
                $contatto_id = get_post_meta($post_ID, '_wpcf_belongs_contatto_id', true);
                $data_inizio_corso_attuale_timestamp = get_post_meta( $contatto_id, 'wpcf-data-inizio-ultimo-corso', true );
                if ( $data_inizio_corso_inserito_timestamp > $data_inizio_corso_attuale_timestamp) {update_post_meta( $contatto_id, 'wpcf-data-inizio-ultimo-corso', $data_inizio_corso_inserito_timestamp );}
    }
}
        add_action( 'save_post', 'inserisce_data_inizio_ultimo_corso', 99 );

Solution:

I assume you are going to check other child "corso" posts of same parent "contatto" post, in your PHP codes.

In your PHP codes, you can get the parent "contatto" post ID by this:
$contatto_id = get_post_meta($post_ID, '_wpcf_belongs_contatto_id', true);

With the "contatto" post ID, you can get all the child "corso" posts with wordpress function get_posts(), by the custom field "_wpcf_belongs_contatto_id", see our document:

https://toolset.com/documentation/customizing-sites-using-php/displaying-child-posts/#displaying-child-post-contents

section "Example of a custom query for displaying child posts"

Relevant Documentation:

https://toolset.com/documentation/customizing-sites-using-php/displaying-child-posts/#displaying-child-post-contents

This support ticket is created 6 years, 1 month 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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)

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by Luo Yang 6 years, 1 month ago.

Assisted by: Luo Yang.

Author
Posts
#619495

Hello i'm using CRED and i have:
parent post type: "contatto" with types custom field "data-inizio-ultimo-corso"
child post type: "corso" with types custom field "data-inizio-corso"
i'm using view filtering only "contatto" post types (because we don't want to see duplicates)

when submitting a new "corso", i'm updating "data-inizio-ultimo -corso" if date greater, using this function:

//DATA INIZIO ULTIMO CORSO
function inserisce_data_inizio_ultimo_corso( $post_ID ) {     
    if ( get_post_type( $post_ID ) == 'corso' ) {
                $data_inizio_corso_inserito_timestamp = get_post_meta($post_ID, 'wpcf-data-inizio-corso', true);
                $contatto_id = get_post_meta($post_ID, '_wpcf_belongs_contatto_id', true);
                $data_inizio_corso_attuale_timestamp = get_post_meta( $contatto_id, 'wpcf-data-inizio-ultimo-corso', true );
                if ( $data_inizio_corso_inserito_timestamp > $data_inizio_corso_attuale_timestamp) {update_post_meta( $contatto_id, 'wpcf-data-inizio-ultimo-corso', $data_inizio_corso_inserito_timestamp );}
    }
}
        add_action( 'save_post', 'inserisce_data_inizio_ultimo_corso', 99 );

Until now everything is working good.

My problem is that if we delete or update a "corso", the field "data-ultimo-corso" (in case of multiple corsi) result wrong.

I would, deleting or editing a "corso", to check other existing dates (of right parent) and update "data-ultimo-corso".

So i added this function:

/*RESINSERISCE DATA INIZIO ULTIMO CORSO SU  DELETE CORSO*/
add_action( 'before_delete_post', 'reinserisce_data_inizio_ultimo_corso' );
function reinserisce_data_inizio_ultimo_corso( $post_ID ) {     
    if ( get_post_type( $post_ID ) == 'corso' ) {
		$data_inizio_corso_ora = "";
                $contatto_id = get_post_meta($post_ID, '_wpcf_belongs_contatto_id', true);
                
               update_post_meta( $contatto_id, 'wpcf-data-inizio-ultimo-corso', $data_inizio_corso_ora );
    }
}

Now i can delete custom field "data-ultimo-corso" but i lack the code to check other parent fields.

Could you help me to achieve this?

Thanks

#619599

Dear CulturaI,

I assume you are going to check other child "corso" posts of same parent "contatto" post, in your PHP codes.

In your PHP codes, you can get the parent "contatto" post ID by this:
$contatto_id = get_post_meta($post_ID, '_wpcf_belongs_contatto_id', true);

With the "contatto" post ID, you can get all the child "corso" posts with wordpress function get_posts(), by the custom field "_wpcf_belongs_contatto_id", see our document:

https://toolset.com/documentation/customizing-sites-using-php/displaying-child-posts/#displaying-child-post-contents

section "Example of a custom query for displaying child posts"

#620704

Hello Luo and thanks for helping me, you assumed right.
I Tried to follow documentation and edited my code:

/*REINSERISCE DATA INIZIO ULTIMO CORSO SU  DELETE CORSO*/
add_action( 'before_delete_post', 'reinserisce_data_inizio_ultimo_corso' );
function reinserisce_data_inizio_ultimo_corso( $post_ID ) {     
    if ( get_post_type( $post_ID ) == 'corso' ) {
        $data_inizio_corso_ora = "";
        $contatto_id = get_post_meta($post_ID, '_wpcf_belongs_contatto_id', true);
     
$child_args = array(
'post_type' => 'corso',
'numberposts' => -1,
'order' => 'ASC',
'meta_query' => array(array('key' => '_wpcf_belongs_contatto_id', 'value' => $contatto_id))
);
  $child_post = get_posts($child_args);
     
foreach ($child_post as $post) {
  $data_corso_rilevata= types_render_field("data-inizio-corso" , array("id"=> "$post->ID", "output" => "raw"));
  if ($data_corso_rilevata > $data_inizio_corso_ora ){$data_inizio_corso_ora = $data_corso_rilevata;}
          
 } 
     
     
               update_post_meta( $contatto_id, 'wpcf-data-inizio-ultimo-corso', $data_inizio_corso_ora );
    }

i can update "data-ultimo-corso", but i would to hook my action after post deletion not before, it's possible with cred? I don'see deletion hooks in documentation

Thanks

#620770

Ok resolved excluding current post, here my final code for other peoples:

/*REINSERISCE DATA INIZIO ULTIMO CORSO SU  DELETE CORSO*/
add_action( 'before_delete_post', 'reinserisce_data_inizio_ultimo_corso');
function reinserisce_data_inizio_ultimo_corso( $post_ID ) {     
    if ( get_post_type( $post_ID ) == 'corso' ) {
        $data_inizio_corso_ora = "";
        $contatto_id = get_post_meta($post_ID, '_wpcf_belongs_contatto_id', true);
     
$child_args = array(
'post_type' => 'corso',
'numberposts' => -1,
'post__not_in' => array( $post_ID ),
'meta_query' => array(array('key' => '_wpcf_belongs_contatto_id', 'value' => $contatto_id))
);
  $child_post = get_posts($child_args);
     
foreach ($child_post as $post) {
     $data_corso_rilevata= types_render_field("data-inizio-corso" , array("id"=> "$post->ID", "output" => "raw"));
  if ($data_corso_rilevata > $data_inizio_corso_ora ){$data_inizio_corso_ora = $data_corso_rilevata;}
          
 } 
     
     
               update_post_meta( $contatto_id, 'wpcf-data-inizio-ultimo-corso', $data_inizio_corso_ora );
    }
}

Thanks again for support!

#620781

You are welcome

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.