Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@MajorRedbeard MajorRedbeard commented Dec 16, 2016

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

This comment has been minimized.

Copy link

@johannaruiz johannaruiz commented Mar 6, 2017

@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

This comment has been minimized.

Copy link

@tallieven tallieven commented Mar 10, 2017

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

@douglasanro

This comment has been minimized.

Copy link

@douglasanro douglasanro commented Apr 16, 2017

@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;
	}
}
@TheGreenArrow

This comment has been minimized.

Copy link

@TheGreenArrow TheGreenArrow commented Apr 20, 2017

@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

This comment has been minimized.

Copy link

@douglasanro douglasanro commented Apr 22, 2017

@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.

@TheGreenArrow

This comment has been minimized.

Copy link

@TheGreenArrow TheGreenArrow commented Apr 24, 2017

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