Support Forum
Support › MB Frontend Submission › Error while inserting $field_id in rwmb_after_save_postResolved
Hello everyone,
Maybe I haven't followed instructions enough, or I haven't done everything by the book (i am still relatively new with the amazing tools of Metabox), but I am not sure that the rwmb_after_save_post i put at the end of a Metabox is working:
I have to sort some dates from a bootstrap-datepicker ($date_fichier, not really real dates fields, but strings actually). Those dates are a sub-field from a group ($pieces_jointes_et_dates). After carefully reading the documentation, and had being vigilant about the id to put into, I don't think there is any results, maybe because of the denomination of the id. If someone could have a lead, here's the code:
add_action( 'rwmb_pieces_jointes_et_dates_after_save_post', function( $object_id ) {
$dates_to_sort = rwmb_meta( 'pieces_jointes_et_dates');
if ( ! empty( $dates_to_sort ) ) {
foreach ( $dates_to_sort as $group_value ) {
$dates_to_sort = isset( $group_value['date_fichier'] ) ? $group_value['date_fichier'] : '';
//TODO Sorting dates function goes here:
asort($dates_to_sort);
update_post_meta( $object_id, 'pieces_jointes_et_dates', $dates_to_sort );
}
}
} );
Hi Ben,
Can you please share the code that creates the group field and subfields on your site?
pieces_jointes_et_dates
and date_fichier
You can also refer to this topic to know how to get group field and update subfield value in a group
https://support.metabox.io/topic/update_post_meta-for-cloneable-group-fields/#post-36899
Of course, here's the part of the MetaBox used for that:
[
'name' => __( 'Group', 'kallyas' ),
'id' => $prefix . 'pieces_jointes_et_dates',
'type' => 'group',
'collapsible' => true,
'default_state' => 'expanded',
'save_state' => true,
//'group_title' => 'Pièce intitulée {pieces_jointes}, importée le: '. '{date_fichier}',
'group_title' => 'Importée le: {date_fichier}',
'clone' => true,
//'clone_default' => false,
//'clone_as_multiple' => false,
'sort_clone' => true,
'clone_default' => false,
'add_button' => esc_html__( 'Ajouter une autre pièce jointe', 'kallyas' ),
'fields' => [
[
'name' => __( 'Pièce(s) jointe(s)', 'kallyas' ),
'id' => $prefix . 'pieces_jointes',
'type' => 'file_upload',
],
[
'name' => __( 'Date de dépôt du fichier', 'kallyas' ),
'id' => $prefix . 'date_fichier',
'type' => 'bs_date',
//'std' => 'Entrez ici la date de saisie de la pièce',
'js_options' => array(
'format' => "dd/mm/yyyy",
'endDate' => "today",
'startView' => 0,
'minViewMode' => 0,
'maxViewMode' => 2,
),
'required' => 1,
],
],
],
Hi Ben,
So do you want to sort the cloneable group based on the subfield value (date)? Meta Box does not support a field type bs_date
so I'm not sure what exactly the field value is but the main goal is, you need to update the group value outside the loop. For example:
add_action( 'rwmb_pieces_jointes_et_dates_after_save_post', function( $object_id ) {
$group_values = rwmb_meta( 'pieces_jointes_et_dates', '', $object_id );
if ( ! empty( $group_values ) ) {
foreach ( $group_values as $group_key => $group_value ) {
$dates_to_sort = isset( $group_value['date_fichier'] ) ? $group_value['date_fichier'] : '';
// do something here to update the subfield value for each clone group
$group_values[$group_key]['date_fichier'] = '9/2/2022';
}
}
//update the whole group value
update_post_meta( $object_id, 'pieces_jointes_et_dates', $group_values );
} );
Thanks a lot for your knowledge and your time! 😉
hi ben06,
you can use the following code for sorting your group by date
add_action( 'rwmb_pieces_jointes_et_dates_after_save_post', function($object_id){
$pj = $_POST['pieces_jointes_et_dates'];
//sorting by date with usort
usort($pj, function($a, $b) {
//convert date_fichier DD/MM/YYYY with datetime to get a timestamp that you can sort
$a_date = DateTime::createFromFormat('d/m/Y', $a['date_fichier']);
$b_date = DateTime::createFromFormat('d/m/Y', $b['date_fichier']);
return $a_date->getTimestamp() - $b_date->getTimestamp();
});
//Then update the whole group value
update_post_meta($object_id,'pieces_jointes_et_dates', $pj );
} );
Have a nice day 🙂
Hi Luong,
bs_date
is one of my creation, it's a custom field based on Bootstrap datepicker, wich is, in my opinion and experience, far better than jqueryUI datepicker.
Maybe you could have a look on it for a later implementation on MB ?
by the way, thanks for all your fantastic works
have a nice day,
Kevin
Thx a lot again, both of you Long and Kevin! 😉