Skip to content

Instantly share code, notes, and snippets.

@Mte90
Created September 18, 2015 11:27
Show Gist options
  • Save Mte90/708e54b21b1f7372b48a to your computer and use it in GitHub Desktop.
Save Mte90/708e54b21b1f7372b48a to your computer and use it in GitHub Desktop.
Add your cpts to the Widget Activity of the Dashboard in WordPress
<?php
/*
Plugin Name: Dashboard Widget Activity Custom Post Type
Plugin URI:
Description:
Author: Daniele Mte90 Scasciafratte
Version: 1.0.0
Author URI: http://mte90.net
*/
//Add the support for your cpt in the Widget Activity of the Admin Dashboard
if ( is_admin() ) {
add_filter( 'dashboard_recent_posts_query_args', 'add_page_to_dashboard_activity' );
function add_page_to_dashboard_activity( $query_args ) {
if ( is_array( $query_args[ 'post_type' ] ) ) {
//Set yout post type
$query_args[ 'post_type' ][] = 'page';
} else {
$temp = array( $query_args[ 'post_type' ], 'page' );
$query_args[ 'post_type' ] = $temp;
}
return $query_args;
}
}
@MajorRedbeard
Copy link

This displays the custom posts in the activity, though what I'm seeing is it's displaying the oldest 3. Is it possible to modify this to show the newest posts?

@johannaruiz
Copy link

@MajorRedbeard just $query_args['order'] = 'DESC'; $query_args['orderby'] = 'date';. If you want to update how many posts are displayed update$query_args['posts_per_page'] = $int; It's that easy. The dashboard_recent_posts_query_args hook accepts all of the same arguments as WP_Query.

@tallieven
Copy link

Thanks.. it works for just one kind of custom post type...
What if I have two diferent custom post types?

@douglasanro
Copy link

@tallieven try that way:

if ( is_admin() ) {
	add_filter( 'dashboard_recent_posts_query_args', 'add_page_to_dashboard_activity' );
	function add_page_to_dashboard_activity( $query ) {
		// Return all post types
		$post_types = get_post_types();
		// Return post types of your choice
		// $post_types = ['post', 'foo', 'bar'];
		if ( is_array( $query['post_type'] ) ) {
			$query['post_type'] = $post_types;
		} else {
			$temp = $post_types;
			$query['post_type'] = $temp;
		}
		return $query;
	}
}

@AkremBelkahla
Copy link

@douglasanro
Is it possible to add post type name and author next to post name ? and increase the number of last posts (more than 5) ?
Thanks !

@douglasanro
Copy link

@thegreenarrow
Well, the default dashboard_recent_posts_query_args accepts all WP_query arguments, like posts_per_page to increase the number of posts see here. But, this function only return date and title.

The easy way to display other fields is creating a custom dashboard widget, like this, and add the loop with all fields you wish (you can use the core function loop), and implement post type and author fields.

@AkremBelkahla
Copy link

Thank you, that's exactly what i need !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment