Skip to content

Instantly share code, notes, and snippets.

@stefancrowe
Created January 17, 2019 12:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefancrowe/80e9761891cc047ddf4263936d12fe59 to your computer and use it in GitHub Desktop.
Save stefancrowe/80e9761891cc047ddf4263936d12fe59 to your computer and use it in GitHub Desktop.
WordPress Advanced Custom Fields - Organise Field Groups into categories with sort
/**
* Add support for Field Group categories to Advanced Custom Fields (WordPress plugin).
*
* Drop this code into your theme's functions.php or equivalent file. This will
* then allow you to add categories to Field Groups on the edit screen. It also
* adds a "Category" column to the Field Groups list page - users can click
* on the category name to filter the Field Groups by that category.
*
* @author Stefan Crowe <me@stefancrowe.co.uk>
*/
/**
* Register ACF Field Group category
*/
function register_acf_field_category()
{
$args = array(
'hierarchical' => true,
'public' => false,
'show_ui' => true,
'show_in_nav_menus' => true,
);
register_taxonomy( 'acf-field-category', array( 'acf-field-group' ), $args );
}
add_action('init', 'register_acf_field_category', 0);
function acf_field_group_columns($columns)
{
$columns['acf-field-category'] = __('Category');
return $columns;
} // end function reference_columns
add_filter('manage_edit-acf-field-group_columns', 'acf_field_group_columns', 20);
function acf_field_group_columns_content($column, $post_id)
{
switch ($column) {
case 'acf-field-category':
global $post;
$terms = get_the_terms($post->ID, 'acf-field-category');
if ($terms) {
$term = array_pop($terms);
echo '<a href="?post_type=acf-field-group&acf-field-category=' . $term->slug . '">' . $term->name . '</a>';
} else {
echo '-';
}
break;
default:
break;
} // end switch
} // end function reference_columns_content
add_action('manage_acf-field-group_posts_custom_column', 'acf_field_group_columns_content', 20, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment