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

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


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 expertise acronyms. I proceed to broaden the record 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 practice put up 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 put up kind, 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 problem, 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 possibly can add a customized discipline to your admin columns. On this case, I’m solely doing it for the acronym customized put up kind. You’ll wish to replace the textdomain in your code in your theme or little one theme.

// Add a brand new 'Definition' column to the Acronym put up record
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 discipline
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 truly would love it to be the second column, so I modified the code so as to add the column after the title column.

Ads

// Add a brand new 'Definition' column to the Acronym put up record
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 discipline
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 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 record:

// Add a brand new 'Definition' column to the Acronym put up record
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 discipline
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

- Advertisment -
Google search engine

Most Popular

Recent Comments