• Resolved Ghostwise_

    (@ghostwise_)


    Hello folks,

    – I have a custom field (via the ACF plugin) called Counts.

    – It has a Field Type set to “numbers”, because it stores numbers. D’uh.

    – I managed to have it show up as a new column in the “Posts -> All Posts” view on the admin interface (/wp-admin/edit.php). The column correctly indicates the Counts value for each post, which is great. This is derived from Eliot Akira’s post.

    – The column is sortable. Because my end objective is to order all my posts by their Counts value (asc. or desc.)

    – But right now clicking on the sort button sorts the posts… by their age or ID number (e.g. the first posts I created come up, or the latest posts) rather than by their Counts value (??)

    I’m out of energy after having come this far with my meagre skills, though. 🙁 Somebody help me with the last few steps ?

    Rachel Carden at WPDreamer has something worked out, but the meta_key thing defeats me.

    My current function :

    //////////////////////////////
    //Add custom column using the Counts field to the posts admin display
    //////////////////////////////
    add_filter('manage_edit-post_columns', 'my_columns_head');
    function my_columns_head($defaults) {
    $defaults['Counts'] = 'Views';
    return $defaults;
    }
    //Add rows data
    add_action( 'manage_post_posts_custom_column' , 'my_custom_column', 10, 2 );
    function my_custom_column($column, $post_id ){
    switch ( $column ) {
    case 'Counts':
    $views_value = get_field( 'field_5465eef2489a9', $post_id );
    echo $views_value;
    break;
    }
    }
    
    // Make these columns sortable
    function sortable_columns() {
      return array(
        'Counts'      => 'Counts'
      );
    }
    
    add_filter( "manage_edit-post_sortable_columns", "sortable_columns" );
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Ghostwise_

    (@ghostwise_)

    I had a brief “what if ?” moment and turned echo $views_value; into echo intval $views_value; because heh, maybe the value wasn’t returned as a numerical one ?

    Nope.

    Thread Starter Ghostwise_

    (@ghostwise_)

    Latest flailing attempt, using orderby on the meta key but clearly missing something :

    //////////////////////////////
    //Add custom column using the Counts field to the posts admin display
    //////////////////////////////
    add_filter('manage_edit-post_columns', 'my_columns_head');
    function my_columns_head($columns) {
    $columns['Counts'] = 'Counts';
    return $columns;
    }
    //Add rows data
    add_action( 'manage_post_posts_custom_column' , 'my_custom_column', 10, 2 );
    function my_custom_column($column, $post_id ){
    switch ( $column ) {
    case 'Counts':
    $views_value = get_field( 'field_5465eef2489a9', $post_id );
    echo intval($views_value);
    break;
    }
    }
    
    // Make these columns sortable
    add_action( 'pre_get_posts', 'views_orderby' );
    function views_orderby( $query ) {
    if ( $query->is_admin() && ( $orderby = $query->get( 'orderby' ) ) ) {
    
          switch( $orderby ) {
          case 'Counts':
    
          $query->set('meta_key','Counts');
          $query->set('orderby','meta_value_num');
    	  break;
        }
      }
    }
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Sorting custom columns in admin interface – I'm *almost* there’ is closed to new replies.