1

I'm trying to order custom posts in admin page using an acf date/time value,

I've found a useful resource on wpmunchies.com

I've made some changes, so my code is the following:

function convertDateFormat($dateString){
    $current_date_A = explode(" ",$dateString);
    $year = $current_date_A[2];
    $monthMap = [
        'Gennaio' => '01',
        'Febbraio' => '02',
        'Marzo' => '03',
        'Aprile' => '04',
        'Maggio' => '05',
        'Giugno' => '06',
        'Luglio' => '07',
        'Agosto' => '08',
        'Settembre' => '09',
        'Ottobre' => '10',
        'Novembre' => '11',
        'Dicembre' => '12'
    ];
    $month = $monthMap[$current_date_A[1]];
    $day = $current_date_A[0];
    $current_time = explode(".",$current_date_A[3]);
    $hour = $current_time[0];
    $minute = $current_time[1];
    $current_date_F = $year . '-' . $month . '-' . $day . ' ' . $hour . ':' . $minute . ':00';
    return $current_date_F;
}
function my_page_columns($columns) {
  $columns = array(
      'cb'  => '<input type="checkbox" />',
      'title'   => 'Title',
      'dataora'     => 'Data',
  );
  return $columns;
}

function my_custom_columns($column) {
  global $post;
  if($column == 'dataora') {
    $curdata = convertDateFormat(get_field('data'));
    $date = DateTime::createFromFormat('Y-m-d H:i:s', $curdata);
    if( empty( $date ) ) :
     echo 'No Date';
    else:
     echo $date->format('j F H.i');
    endif;
  }
}

add_action("manage_proiezione_posts_custom_column", "my_custom_columns");
add_filter("manage_edit-proiezione_columns", "my_page_columns");

function my_column_register_sortable( $columns ) {
  $columns['dataora'] = 'dataora';
  return $columns;
}

add_filter("manage_edit-proiezione_sortable_columns", "my_column_register_sortable" );

This works with existing posts, but it doesn't with new posts, which are placed at the end of the list, regardless of the value of 'data'

Does anyone have any idea why this happens?

Thanks in advance

2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.