Support

Account

Home Forums General Issues Add Sortable ACF Admin Column Reply To: Add Sortable ACF Admin Column

  • 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