Support

Account

Home Forums General Issues Sorting custom columns with custom fields sorts only by date

Solved

Sorting custom columns with custom fields sorts only by date

  • Hi ACF community and thanks for this amazing plugin and your hard work!

    I have a custom post type – “members”. I’ve created a Field Group with 5 fields and assigned that group to my “members” post type.

    I followed this article to create custom admin columns for “members” – http://www.elliotcondon.com/advanced-custom-fields-admin-custom-columns/

    I’ve created columns:

    function members_columns($columns){
    		$columns = array(
    				'cb'	 	=> '<input type="checkbox" />',
    				'title' 	=> 'Name',
    				'picture'	=> 'Picture',
    				'department'=> 'Department',
    				'sex'       => 'Sex',
    				'featured' 	=> 'Fixed',
    				'staff_order'=> 'Order',
    				'date'		=> 'Date',
    		);
    		return $columns;
    	}

    Populated them with data:

    function members_custom_columns($column){
    		global $post;
    		if($column == 'picture'){
    			echo wp_get_attachment_image( get_field('picture', $post->ID), array(50,50) );
    		}
    		elseif($column == 'featured'){
    			if(get_field('featured')){
    				echo 'Yes';
    			} else {
    				echo '---';
    			}
    		}
    		elseif($column == 'staff_order'){
    			echo get_field('staff_order');
    		}
    		elseif($column == 'department'){
    			echo get_field('department');
    		}
    		elseif($column == 'sex'){
    			echo get_field('sex');
    		}
    	}

    and made them sortable:

     function members_column_register_sortable( $columns ){
    		$columns['featured'] = 'featured';
    		$columns['staff_order'] = 'staff_order';
    		$columns['department'] = 'department';
    		return $columns;
    	}

    And included hooks in my plugin file:

    	add_action("manage_posts_custom_column", "members_custom_columns");
    	add_filter("manage_edit-members_columns", "members_columns");
    	add_filter("manage_edit-members_sortable_columns", "members_column_register_sortable" );

    The corresponding fields types in this field group are:

    ‘featured’ – True / False
    ‘staff_order’ – Number
    ‘department’ – Select

    Result:

    Visually it looks like it should: all the data correctly populating the columns including the pictures, required columns appears to be sortable, and when I click on headers some sorting appears.

    The problem:

    For some reason, sorting works correctly only for original ‘title’ and ‘date’ fields. Whenever I sort by any of the 3 added columns (‘featured’, ‘staff_order’ or ‘department’), it sort them in wrong order.

    After experimenting and testing, it became apparent that each time any of these 3 custom columns were sorted by the order they were published, i.e. by ‘date’.

    One more thing to notice, despite sorting in wrong order, the url parameters seemed to be correct each time, for example if I sort by ‘department’ the url is “/edit.php?post_type=members&orderby=department&order=asc”, yet it sorts by ‘date’.

    When I run same query on my wp_postmeta to see if data returns in correct order, everything is ok.

    Please help!

  • Hi @alex_is

    Sometimes WordPress doesn’t understand that you need to order by certain post meta. To fix it, please take a look at this page: http://code.tutsplus.com/articles/quick-tip-make-your-custom-column-sortable–wp-25095.

    Hope this helps.

  • Ahh brilliant, that did it for me. Thank you, James!

    For my case I’ve added:

            add_action( 'pre_get_posts', 'custom_orderby' );
    	function custom_orderby( $query ) {
    		if( ! is_admin() )
    			return;
    
    		$orderby = $query->get( 'orderby');
    
    		if( 'featured' == $orderby ) {
    			$query->set('meta_key','featured');
    			$query->set('orderby','meta_value');
    		}
    		elseif ( 'staff_order' == $orderby ) {
    			$query->set('meta_key','staff_order');
    			$query->set('orderby','meta_value_num');
    		}
    		elseif ( 'department' == $orderby ) {
    			$query->set('meta_key','department');
    			$query->set('orderby','meta_value');
    		}
    	} 

    I think this last function has to be added to the original Elliots article, maybe someone can pass it along :))

    Thanks so much again!!!

  • Interesting topic that helped me sort my /true/false custom fields column.
    It’s all working, but the initial sorting for true / false fields will be asc and we want desc.
    Adding this code under each conditional will fix that :

            $orderdir = $query->get( 'order');
            if('desc' == $orderdir) {
                $query->set('order','asc');
            } else {    
                $query->set('order','desc');
            }
    

    final result :

    		
    if( 'featured' == $orderby ) {
    $query->set('meta_key','featured');
    $query->set('orderby','meta_value');
    $orderdir = $query->get( 'order');
      if('desc' == $orderdir) {
           $query->set('order','asc');
          } else {    
            $query->set('order','desc');
           }
    }
    
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Sorting custom columns with custom fields sorts only by date’ is closed to new replies.