Displaying Custom Post Types In “At A Glance” Meta Box

I found the following snippet will displays the number of Custom Post Types published in Dashboard’s At A Glance widget, like this:

At A Glance

Read More

Is there a way to turn that “81 Wrestlers” text to a link to the post types list. Here’s the code:

add_filter( 'dashboard_glance_items', 'custom_glance_items', 10, 1 );
function custom_glance_items( $items = array() ) {
    $post_types = array( 'wrestler' );
    foreach( $post_types as $type ) {
        if( ! post_type_exists( $type ) ) continue;
        $num_posts = wp_count_posts( $type );
        if( $num_posts ) {
            $published = intval( $num_posts->publish );
            $post_type = get_post_type_object( $type );
            $text = _n( '%s ' . $post_type->labels->singular_name, '%s ' . $post_type->labels->name, $published, 'your_textdomain' );
            $text = sprintf( $text, number_format_i18n( $published ) );
            if ( current_user_can( $post_type->cap->edit_posts ) ) {
                $items[] = sprintf( '%2$s', $type, $text ) . "n";
            } else {
                $items[] = sprintf( '%2$s', $type, $text ) . "n";
            }
        }
    }
    return $items;
}

Related posts

Leave a Reply

4 comments

  1. Here is the function that I use to display CPT in the “At a glance” widget

    add_action( 'dashboard_glance_items', 'cpad_at_glance_content_table_end' );
    function cpad_at_glance_content_table_end() {
        $args = array(
            'public' => true,
            '_builtin' => false
        );
        $output = 'object';
        $operator = 'and';
    
        $post_types = get_post_types( $args, $output, $operator );
        foreach ( $post_types as $post_type ) {
            $num_posts = wp_count_posts( $post_type->name );
            $num = number_format_i18n( $num_posts->publish );
            $text = _n( $post_type->labels->singular_name, $post_type->labels->name, intval( $num_posts->publish ) );
            if ( current_user_can( 'edit_posts' ) ) {
                $output = '<a href="edit.php?post_type=' . $post_type->name . '">' . $num . ' ' . $text . '</a>';
                echo '<li class="post-count ' . $post_type->name . '-count">' . $output . '</li>';
            }
        }
    }
    

    This make the text clickable as link. Hope this helps

  2. Okay, so I used this code to only display “wrestler” post type & it worked. I mixed mine & Pieter Goosen’s code to get this out:

    add_filter( 'dashboard_glance_items', 'custom_glance_items', 10, 1 );
    function custom_glance_items( $items = array() ) {
        $post_types = array( 'wrestler' );
        foreach( $post_types as $type ) {
            if( ! post_type_exists( $type ) ) continue;
            $num_posts = wp_count_posts( $type );
            if( $num_posts ) {
                $published = intval( $num_posts->publish );
                $post_type = get_post_type_object( $type );
                $text = _n( '%s ' . $post_type->labels->singular_name, '%s ' . $post_type->labels->name, $published, 'your_textdomain' );
                $text = sprintf( $text, number_format_i18n( $published ) );
                if ( current_user_can( $post_type->cap->edit_posts ) ) {
                $output = '<a href="edit.php?post_type=' . $post_type->name . '">' . $text . '</a>';
                    echo '<li class="post-count ' . $post_type->name . '-count">' . $output . '</li>';
                } else {
                $output = '<span>' . $text . '</span>';
                    echo '<li class="post-count ' . $post_type->name . '-count">' . $output . '</li>';
                }
            }
        }
        return $items;
    }
    
  3. For all future occurrences, of adding custom post types to the ‘At a Glance’ box, the following code worked for me in WordPress 4.6.1. And it may work for others.

    // Add custom taxonomies and custom post types counts to dashboard
        add_action( 'dashboard_glance_items', 'cpt_to_at_a_glance' );
    function cpt_to_at_a_glance() {
            // Custom post types counts
            $post_types = get_post_types( array( '_builtin' => false ), 'objects' );
            foreach ( $post_types as $post_type ) {
                $num_posts = wp_count_posts( $post_type->name );
                $num = number_format_i18n( $num_posts->publish );
                $text = _n( $post_type->labels->singular_name, $post_type->labels->name, $num_posts->publish );
                if ( current_user_can( 'edit_posts' ) ) {
                    $num = '<li class="post-count"><a href="edit.php?post_type=' . $post_type->name . '">' . $num . ' ' . $text . '</a></li>';
                }
                echo $num;
            }
        }
    

    All credit goes to the following author

  4. In code you posted I can’t really understand whats the point of:

    if ( current_user_can( $post_type->cap->edit_posts ) ) {
      $items[] = sprintf( '%2$s', $type, $text ) . "n";
    } else {
      $items[] = sprintf( '%2$s', $type, $text ) . "n";
    }
    

    I.E. if the current user can edit the post type do something, otherwise do same thing…

    I guess you want to show the link to posts list if the current user can edit post type (just like WordPress does for pages and posts).

    In that case your code becomes:

    function custom_glance_items( $items = array() ) {
      $post_types = array( 'wrestler' );
      foreach( $post_types as $type ) {
        if( ! post_type_exists( $type ) ) continue;
        $num_posts = wp_count_posts( $type );
        if( $num_posts ) {
          $published = intval( $num_posts->publish );
          $post_type = get_post_type_object( $type );
          $text = _n(
            '%s ' . $post_type->labels->singular_name,
            '%s ' . $post_type->labels->name,
            $published,
            'your_textdomain'
          );
          $text = sprintf( $text, number_format_i18n( $published ) );
    
          // show post type list id user can edit the post type,
          // otherwise just swho the name and the count
          if ( current_user_can( $post_type->cap->edit_posts ) ) {
            $edit_url = add_query_arg( array('post_type' => $type),  admin_url('edit.php') );
            $items[] = sprintf( '<a href="%s">%s</a>', $edit_url, $text ) . "n";
          } else {
            $items[] = $text . "n";
          }
    
        } // end if( $num_posts )
      } // end foreach
      return $items;
    }