Friday, May 12, 2023
HomeMobile MarketingWordPress: How To Add A Sortable Column To Show A Customized Area...

WordPress: How To Add A Sortable Column To Show A Customized Area On The Customized Put up Sort Admin Web page


One space of Martech Zone that continues to drive numerous site visitors to my website is my rising documentation of gross sales, advertising, and know-how acronyms. I proceed to increase the checklist of virtually 600 and I additionally tag the posts with the acronym in order that the newest posts are displayed throughout the single acronym show web page.

This tradition publish sort I created has three vital parts:

  • 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 parts to any publish sort, so the definition needed to be added by way of a customized discipline that’s integrated by way of Meta Field. There’s one excellent concern, although, and that’s displaying the definition on the admin web page the place all of my Acronyms are listed. 

Inside your capabilities.php file, you’ll be able to add a customized discipline to your admin columns. On this case, I’m solely doing it for the acronym customized publish sort. You’ll wish to replace the textdomain in your code in your theme or youngster theme.

// Add a brand new 'Definition' column to the Acronym publish checklist
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 discipline
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 love 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 checklist
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 discipline
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 via my acronyms and think about 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 embrace the sortable aspect in addition to the question that’s wanted to prefetch the checklist:

// Add a brand new 'Definition' column to the Acronym publish checklist
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 discipline
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 types 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');
    }
}



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments