How to add a custom column in WordPress post/page/custom-post-type list

Recently I encountered a problem where I need to search a text on all the pages and I wanted to add a custom column in the page list where yes or no will be shown. If a page contains that string, “Yes” will be shown otherwise “No” will be shown in the column. It solves two problem

1. Adding a custom column in the page list
2. Search a text on all the pages

By the end of this article, you will be able to add a custom column anywhere (post, page or custom-post-type) and you can search a text on a page or post contents.

Adding a custom column

We need two things, a filter hook manage_page_posts_columns and an action hook manage_page_posts_custom_column. The filter hook manage_page_posts_columns filters the columns of the post list table for a specific post type (e.g page, post, product or any custom-post-type) and the action hook manage_page_posts_custom_column adds content to that column. As I said earlier, I want to show “Yes”/”No” in the column.

Paste this code in your active theme’s functions.php. This code will add a column in the page list named “ESS Shortcode”.

add_filter('manage_page_posts_columns', function($columns) {
	return array_merge($columns, ['shortcode_col' => __('ESS Shortcode', 'textdomain')]);
});

The common form of this filter hook is manage_{$post_type}_posts_columns. You should replace {$post_type} with your post type or custom post type.

Let’s add content to the column.

add_action('manage_page_posts_custom_column', function($column_key, $post_id) {
	if ($column_key == 'shortcode_col') {
	    //$check = output_all_postmeta($post_id); 
            $check = 'FOUND'; // we will change this code later.
		if ($check == 'FOUND') {
			echo '<span style="color:green;">'; _e('Yes', 'textdomain'); echo '</span>';
		} else {
			echo '<span style="color:red;">'; _e('No', 'textdomain'); echo '</span>';
		}
	}
}, 10, 2); 

Search a text on all the pages

function output_all_postmeta($id) {

	$postmetas = get_post_meta($id);
       
    $search_string='ess_grid';
    $full_content = '';
    $full_content .= get_the_content($id);  // get the contents from default editor
    
         // get values of all post metas
	foreach($postmetas as $meta_key=>$meta_value) { 
          $full_content .= $meta_key . ' : ' . $meta_value[0]; 
          //echo '<pre>'. $meta_key . ' : ' . $meta_value[0] . '<br/></pre>'; 
        }
	if( strstr( $full_content, $search_string ) ){
        return 'FOUND';    
    }else{
        return 'NOT';
        }
}

In this function with this line $search_string='ess_grid'; we will search for ‘ess_grid’ text in a post content. We get the full content of WordPress default editor by this code $full_content .= get_the_content($id);. In my case, my client used a premium theme called SimpleMag. In this theme, some contents can be added by the post meta fields so we searched on all postmetas with a foreach loop.

Finally, we check whether the text ‘ess_grid’ is available in any of the post meta values or the default content. Your function should be different according to your need.

If you have any trouble figuring out your custom function, just let me know. I’ll try my best to help you.

Leave a Comment

Your email address will not be published.