Support

Account

Home Forums General Issues Add Sortable ACF Admin Column

Helping

Add Sortable ACF Admin Column

  • I’m looking to add a very simple sort-able custom admin column from the “select” type. I simply want the “Primary Category” field to show in the admin column, and I want to be able to sort it alphabetically between the options (Dining, Lodging, Bars). Info:

    Custom post type – “venues”
    ACF field – “priamry_category” (“select” type, willing to change if needed)

    I’ve been reading a lot about doing this for awhile now, but I can’t quite find what I’m looking for. I saw there was a plugin for making the fields sort-able, and I’d prefer to avoid using that, as the code addition should be just a few lines. If someone could walk me through this, I’d be grateful.

    Here’s my current code, which shows the fields and proper value, I just need sorting now.

    EDIT

    I seem to have figured it out. I’m not sure why “edit-venues” has to go in the last piece of code, but it wouldn’t work unless I did, despite a tutorial showing “edit-type” being in the first filter as well. Any ideas? (seems to be working, regardless).

    add_filter('manage_venues_posts_columns', 'filter_venues_custom_columns');
    
    function filter_venues_custom_columns($columns) {
        $columns['primary_category'] = 'Primary Category';
        return $columns;
    }
    
    add_action('manage_venues_posts_custom_column',  'action_venues_custom_columns');
    
    function action_venues_custom_columns($column) {
    	global $post;
    	if($column == 'primary_category') {
    		echo(get_field('primary_category', $post->ID));
    	}
    }
    
    add_filter( 'manage_edit-venues_sortable_columns', 'sortable_venues_custom_columns' );
    
    function sortable_venues_custom_columns( $columns ) {
        $columns['primary_category'] = 'title';
     
        //To make a column 'un-sortable' remove it from the array
        //unset($columns['date']);
     
        return $columns;
    }
  • It looks like you have everything you need except a `pre_get_posts’ filter.

    I recently did this on a site I was working on. Basically, you need to add a filter.

    Here is the filter that I used:

    
        function reference_pre_get_posts($query) {
          // only in admin
          if (!is_admin() || !$query->is_main_query()) {
            return;
          }
          if ($query->query_vars['post_type'] == 'reference' && 
              ($orderby = $query->get('orderby'))) {
            switch ($orderby) {
              case 'featured':
              case 'reference_featured':
                $query->set('meta_key', 'reference_featured');
                $query->set('orderby', 'meta_value_num');
                break;
              default:
                // do nothing
                break;
            }
          } // end if reference and orderby
        }
    

    basically it test to see if we’re in the admin and if we are querying my post type and it it is one of the meta_keys I’ve set up as a sortable field. If it is then I add the meta_key that it needs to be ordered by and change the orderby to meta_value.

    Hope this helps

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Add Sortable ACF Admin Column’ is closed to new replies.