1

I am creating a hit counter that counts the number of page views across all post types. I have gotten it to work and the column displays for each post type showing the total number of page views. I was able to get the column to show up for every post type by using the following filter.

add_filter('manage_posts_columns', 'posthit_columns');

I want to make this a portable column. I have gotten this to work for the main posts, post type, but i dont know how to make it show up for every post type without actually creating a filter for every post type.

The following filter will make the column sortable for the default post, but i have several post types and this isn't working. Is there a catch all filter for this?

add_filter( 'manage_edit-post_sortable_columns', 'managesortablecolumns' );
2

Why not get all post_types and loop through them with the filter. This should work if the callback for all post types is the same (managesortablecolumns).

The code will look something like this:

function init_managesortablecolumns (){
    $post_types = get_post_types();
    foreach( $post_types as $post_type ){
        add_filter( 'manage_edit-' . $post_type . '_sortable_columns', 'managesortablecolumns' );
    }
}
add_action( 'init', 'init_managesortablecolumns' );

BTW, if you store the post hit value in a custom field (post meta) you could use the Admin Columns Pro plugin. This plugin has an interface for adding new columns to the overview with sorting, filtering and inline edit support.

  • good idea, but it doesnt seem to have worked for me. I have no way to check, but does get_post_types return anything in the init hook? – Dan Hastings Apr 11 '16 at 9:57
  • sorted the issue. Changed the priority of the init hook so that it loaded later. the custom posts were not loaded at the time that this init hook ran – Dan Hastings Apr 11 '16 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.