WordPress.org

Forums

[resolved] [closed] Recent Drafts on Dashboard to include Custom Post types (4 posts)

  1. bekar09
    Member
    Posted 2 years ago #

    Hi,

    I have come across many examples of including custom content type and custom taxonomy counts in the Right Now widget in the Dashboard. The one that I referred is below:
    http://wpsnipp.com/index.php/functions-php/include-custom-post-types-in-right-now-admin-dashboard-widget/

    Now what I want is to include the latest 10 drafts (both post and custom post type) to be shown in the Recent Drafts widget.

    How do I do this?

    Thanks.

  2. Alicia
    Member
    Posted 2 years ago #

    There is no action to hook into for recent drafts, so you need to copy it from WordPress core and modify it in your theme.
    Add this to your theme functions.php

    Removes default recent drafts widget:

    function disable_default_dashboard_widgets() {
        remove_meta_box('dashboard_recent_drafts', 'dashboard', 'core');
    }
    add_action('admin_menu', 'disable_default_dashboard_widgets');

    Adds back the recent drafts widget, but modified to show all post types and 10 posts.

    function my_custom_dashboard_widgets() {
        global $wp_meta_boxes;
        wp_add_dashboard_widget('custom_recent_drafts', 'Recent Drafts (All Post Types)', 'custom_dashboard_recent_drafts');
    }  
    
    function custom_dashboard_recent_drafts( $drafts = false ) {
    	if ( !$drafts ) {
    		$drafts_query = new WP_Query( array(
    			'post_type' => 'any',
    			'post_status' => 'draft',
    			'author' => $GLOBALS['current_user']->ID,
    			'posts_per_page' => 10,
    			'orderby' => 'modified',
    			'order' => 'DESC'
    		) );
    		$drafts =& $drafts_query->posts;
    	}
    
    	if ( $drafts && is_array( $drafts ) ) {
    		$list = array();
    		foreach ( $drafts as $draft ) {
    			$url = get_edit_post_link( $draft->ID );
    			$title = _draft_or_post_title( $draft->ID );
    			$item = "<h4><a href='$url' title='" . sprintf( __( 'Edit ā€œ%sā€' ), esc_attr( $title ) ) . "'>" . esc_html($title) . "</a> <abbr title='" . get_the_time(__('Y/m/d g:i:s A'), $draft) . "'>" . get_the_time( get_option( 'date_format' ), $draft ) . '</abbr></h4>';
    			if ( $the_content = preg_split( '#[\r\n\t ]#', strip_tags( $draft->post_content ), 11, PREG_SPLIT_NO_EMPTY ) )
    				$item .= '<p>' . join( ' ', array_slice( $the_content, 0, 10 ) ) . ( 10 < count( $the_content ) ? 'ā€¦' : '' ) . '</p>';
    			$list[] = $item;
    		}
    ?>
    	<ul>
    		<li><?php echo join( "</li>\n<li>", $list ); ?></li>
    	</ul>
    	<p class="textright"><a href="edit.php?post_status=draft" ><?php _e('View all'); ?></a></p>
    <?php
    	} else {
    		_e('There are no drafts at the moment');
    	}
    }
    
    add_action('wp_dashboard_setup', 'my_custom_dashboard_widgets');
  3. bekar09
    Member
    Posted 2 years ago #

    Perfect solution @amduffy.

    Thanks for your help.

  4. Ov3rfly
    Member
    Posted 1 year ago #

    Another solution could be a general hook for WP_Query like this:

    function my_custom_recent_drafts( $query ) {
    	if (
    		isset( $query->query_vars['post_status'] ) && $query->query_vars['post_status'] == 'draft'
    		&& isset( $query->query_vars['post_type'] ) && $query->query_vars['post_type'] == 'post'
    		&& isset( $query->query_vars['author'] ) && $query->query_vars['author'] == $GLOBALS['current_user']->ID
    		&& isset( $query->query_vars['posts_per_page'] ) && $query->query_vars['posts_per_page'] == 5
    		&& isset( $query->query_vars['orderby'] ) && $query->query_vars['orderby'] == 'modified'
    		&& isset( $query->query_vars['order'] ) && $query->query_vars['order'] == 'DESC'
    	) {
    		// show all post types
    		$query->query_vars['post_type'] = 'any';
    		// show 10 drafts
    		$query->query_vars['posts_per_page'] = 10;
    		// if admin or editor, show drafts of all users
    		if ( current_user_can( 'administrator' ) || current_user_can( 'editor' ) ) {
    			unset( $query->query_vars['author'] );
    		}
    	}
    	return $query;
    }
    add_action( 'pre_get_posts', 'my_custom_recent_drafts' );

    A small add-on here is the check for admin or editor role so they can see all drafts of all users.

    Apologies for hijacking the thread, but maybe the second solution is also helpful for interested readers.

Topic Closed

This topic has been closed to new replies.

About this Topic