Support

Account

Home Forums ACF PRO Sorting CPT admin columns by ACF Date Picker Reply To: Sorting CPT admin columns by ACF Date Picker

  • Thanks John,

    I’m not sure how to reformat the date but this is a secondary issue to the sorting one, and that’s now fixed. Appreciate your help. The complete code works out as:

    // Change the columns for the edit CPT screen
    
    function change_columns( $cols ) {
      $cols = array(
        'cb'          => '<input type="checkbox" />',
        'event_date'  => __( 'Event Date', 'trans' ),
        'venue'       => __( 'Venue', 'trans' ),
        'city'        => __( 'City', 'trans' ),
        'artist'      => __( 'Artist', 'trans' ),
      );
      return $cols;
    }
    add_filter( "manage_event_posts_columns", "change_columns" );
    
    function custom_columns( $column, $post_id ) {
      switch ( $column ) {
        case "event_date":
          echo get_post_meta( $post_id, 'event_date', true);
          break;
    
        case "venue":
          echo get_post_meta( $post_id, 'venue_name', true);
          break;
    
        case "city":
          echo get_post_meta( $post_id, 'city_&_country', true);
          break;
    
        case "artist":
          echo get_post_meta( $post_id, 'artist', true);
          break;
      }
    }
    add_action( "manage_posts_custom_column", "custom_columns", 10, 2 );
    
    // Make edit screen columns sortable
    
    add_filter( 'manage_edit-event_sortable_columns', 'my_sortable_event_column' );
    
    function my_sortable_event_column( $columns ) {
        $columns['event_date'] = 'event_date';
        $columns['artist'] = 'artist';
    
        return $columns;
    }
    
    add_action( 'pre_get_posts', 'manage_wp_posts_be_qe_pre_get_posts', 1 );
    function manage_wp_posts_be_qe_pre_get_posts( $query ) {
    
       if ( $query->is_main_query() && ( $orderby = $query->get( 'orderby' ) ) ) {
          switch( $orderby ) {
             case 'event_date':
                $query->set( 'meta_key', 'event_date' );
                $query->set( 'orderby', 'meta_value' );      
                break;
    
            case 'artist':
                $query->set( 'meta_key', 'artist' );
                $query->set( 'orderby', 'meta_value' );      
                break;
          }
       }
    }

    I’ll keep looking for a date solution.