WordPress: How To Add A Sortable Column To Show A Customized Discipline On The Customized Publish Kind Admin Web page

News Author


One space of Martech Zone that continues to drive a whole lot of site visitors to my website is my rising documentation of gross sales, advertising and marketing, and expertise acronyms. I proceed to develop the listing of virtually 600 and I additionally tag the posts with the acronym in order that the most recent posts are displayed inside the single acronym show web page.

This practice publish kind I created has three crucial components:

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

With WordPress, the title and content material are included components to any publish kind, so the definition needed to be added by way of a customized subject that’s included by way of Meta Field. There’s one excellent subject, although, and that’s displaying the definition on the admin web page the place all of my Acronyms are listed. 

Inside your features.php file, you possibly can add a customized subject to your admin columns. On this case, I’m solely doing it for the acronym customized publish kind. You’ll wish to replace the textdomain in your code on your theme or little one theme.

// Add a brand new 'Definition' column to the Acronym publish listing
add_filter('manage_acronym_posts_columns', 'add_definition_column_to_acronym_list');
perform 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 brand new 'Definition' column with the values from 'acronym_definition' customized subject
add_action('manage_acronym_posts_custom_column', 'add_definition_column_content_to_acronym_list', 10, 2);
perform 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 provides the column as the primary column on the admin web page. I truly would really like it to be the second column, so I modified the code so as to add the column after the title column.

Commercials

// Add a brand new 'Definition' column to the Acronym publish listing
add_filter('manage_acronym_posts_columns', 'add_definition_column_to_acronym_list');
perform add_definition_column_to_acronym_list($columns) {
    $new_columns = array();

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

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

    return $new_columns;
}

// Fill the brand new 'Definition' column with the values from 'acronym_definition' customized subject
add_action('manage_acronym_posts_custom_column', 'add_definition_column_content_to_acronym_list', 10, 2);
perform 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 simply navigate by way of my acronyms and consider the definition of them:

Display Custom Field for Custom Post Type in WordPress Admin

This added the column however didn’t make it sortable. To make it sortable, the code can embody the sortable ingredient in addition to the question that’s wanted to prefetch the listing:

// Add a brand new 'Definition' column to the Acronym publish listing
add_filter('manage_acronym_posts_columns', 'add_definition_column_to_acronym_list');
perform add_definition_column_to_acronym_list($columns) {
    $new_columns = array();

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

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

    return $new_columns;
}

// Fill the brand new 'Definition' column with the values from 'acronym_definition' customized subject
add_action('manage_acronym_posts_custom_column', 'add_definition_column_content_to_acronym_list', 10, 2);
perform 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');
perform make_definition_column_sortable($columns) {
    $columns['acronym_definition'] = 'acronym_definition';
    return $columns;
}

// Customise the question that kinds the 'Definition' column
add_action('pre_get_posts', 'sort_definition_column');
perform sort_definition_column($question) {
    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');
    }
}