Wednesday, November 15, 2023
HomeMobile MarketingWordPress: How To Add A Sortable Column To Show A Customized Subject...

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


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

This tradition submit kind I created has three essential 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 submit 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 difficulty, 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 submit kind. You’ll wish to replace the textdomain in your code on your theme or baby theme.

// Add a brand new 'Definition' column to the Acronym submit checklist
add_filter('manage_acronym_posts_columns', 'add_definition_column_to_acronym_list');
operate 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);
operate 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 really would love it to be the second column, so I modified the code so as to add the column after the title column.

// Add a brand new 'Definition' column to the Acronym submit checklist
add_filter('manage_acronym_posts_columns', 'add_definition_column_to_acronym_list');
operate 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);
operate 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 means of my acronyms and examine 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 factor in addition to the question that’s wanted to prefetch the checklist:

// Add a brand new 'Definition' column to the Acronym submit checklist
add_filter('manage_acronym_posts_columns', 'add_definition_column_to_acronym_list');
operate 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);
operate 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');
operate make_definition_column_sortable($columns) {
    $columns['acronym_definition'] = 'acronym_definition';
    return $columns;
}

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

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments