Content Marketing

WordPress: How To Add A Sortable Column To Display A Custom Field On The Custom Post Type Admin Page

One area of Martech Zone that continues to drive a lot of traffic to my site is my growing documentation of sales, marketing, and technology acronyms. I continue to expand the list of almost 600 and I also tag the posts with the acronym so that the latest posts are displayed within the single acronym display page.

This custom post type I created has three critical elements:

  • Title – the acronym itself.
  • Definition – what the acronym stands for.
  • Content – the actual description of the acronym.

With WordPress, the title and content are included elements to any post type, so the definition had to be added via a custom field that’s incorporated via Meta Box. There’s one outstanding issue, though, and that’s displaying the definition on the admin page where all of my Acronyms are listed. 

Within your functions.php file, you can add a custom field to your admin columns. In this case, I’m only doing it for the acronym custom post type. You’ll want to update the textdomain in your code for your theme or child theme.

// Add a new 'Definition' column to the Acronym post list
add_filter('manage_acronym_posts_columns', 'add_definition_column_to_acronym_list');
function add_definition_column_to_acronym_list($columns) {
    $new = array();
    foreach($columns as $key => $title) {
        if ($key == 'title') // Put the Definition column after the Title column
            $new['acronym_definition'] = __( 'Definition', 'textdomain' );
        $new[$key] = $title;
    }
    return $new;
}

// Fill the new 'Definition' column with the values from 'acronym_definition' custom field
add_action('manage_acronym_posts_custom_column', 'add_definition_column_content_to_acronym_list', 10, 2);
function add_definition_column_content_to_acronym_list($column, $post_id) {
    if ($column == 'acronym_definition') {
        $definition = get_post_meta($post_id, 'acronym_definition', true);
        if (!empty($definition)) {
            echo $definition;
        } else {
            echo __('No definition', 'textdomain');
        }
    }
}

This adds the column as the first column on the admin page. I actually would like it to be the second column, so I modified the code to add the column after the title column.

// Add a new 'Definition' column to the Acronym post list
add_filter('manage_acronym_posts_columns', 'add_definition_column_to_acronym_list');
function add_definition_column_to_acronym_list($columns) {
    $new_columns = array();

    foreach($columns as $key => $value) {
        $new_columns[$key] = $value;

        if ($key === 'title') {
            $new_columns['acronym_definition'] = __('Definition', 'textdomain');
        }
    }

    return $new_columns;
}

// Fill the new 'Definition' column with the values from 'acronym_definition' custom field
add_action('manage_acronym_posts_custom_column', 'add_definition_column_content_to_acronym_list', 10, 2);
function add_definition_column_content_to_acronym_list($column, $post_id) {
    if ($column === 'acronym_definition') {
        $definition = get_post_meta($post_id, 'acronym_definition', true);
        echo $definition ? $definition : __('No definition', 'textdomain');
    }
}

Now I can easily navigate through my acronyms and view the definition of them:

Display Custom Field for Custom Post Type in WordPress Admin

This added the column but didn’t make it sortable. To make it sortable, the code can include the sortable element as well as the query that’s needed to prefetch the list:

// Add a new 'Definition' column to the Acronym post list
add_filter('manage_acronym_posts_columns', 'add_definition_column_to_acronym_list');
function add_definition_column_to_acronym_list($columns) {
    $new_columns = array();

    foreach($columns as $key => $value) {
        $new_columns[$key] = $value;

        if ($key === 'title') {
            $new_columns['acronym_definition'] = __('Definition', 'textdomain');
        }
    }

    return $new_columns;
}

// Fill the new 'Definition' column with the values from 'acronym_definition' custom field
add_action('manage_acronym_posts_custom_column', 'add_definition_column_content_to_acronym_list', 10, 2);
function add_definition_column_content_to_acronym_list($column, $post_id) {
    if ($column === 'acronym_definition') {
        $definition = get_post_meta($post_id, 'acronym_definition', true);
        echo $definition ? $definition : __('No definition', 'textdomain');
    }
}

// Make the 'Definition' column sortable
add_filter('manage_edit-acronym_sortable_columns', 'make_definition_column_sortable');
function make_definition_column_sortable($columns) {
    $columns['acronym_definition'] = 'acronym_definition';
    return $columns;
}

// Customize the query that sorts the 'Definition' column
add_action('pre_get_posts', 'sort_definition_column');
function sort_definition_column($query) {
    if (!is_admin() || !$query->is_main_query()) {
        return;
    }

    if ($query->get('orderby') == 'acronym_definition') {
        $query->set('meta_key', 'acronym_definition');
        $query->set('orderby', 'meta_value');
    }
}

Douglas Karr

Douglas Karr is CMO of OpenINSIGHTS and the founder of the Martech Zone. Douglas has helped dozens of successful MarTech startups, has assisted in the due diligence of over $5 bil in Martech acquisitions and investments, and continues to assist companies in implementing and automating their sales and marketing strategies. Douglas is an internationally recognized digital transformation and MarTech expert and speaker. Douglas is also a published author of a Dummie's guide and a business leadership book.

Related Articles

Back to top button
Close

Adblock Detected

Martech Zone is able to provide you this content at no cost because we monetize our site through ad revenue, affiliate links, and sponsorships. We would appreciate if you would remove your ad blocker as you view our site.