NB: Make sure you do a back up of your theme, files and database before attempting the tutorials

Number of Posts Published/Edited In the last 48 hours

---- Last modified : 27 November 2021

With this tutorial, we will learn how to create a WP Query which will return the number of posts published or edited during the last 2 days.

With this query, we will ping the Post Type and the results will display the Post Type counter published/edited in the last 2 days.

With our example, the post type is crasyphotosgalleries.
We will add two arguments with the date_query: the first one will check the post_date and/or the post_modified of the last 2 days.

Create a query - Number of posts published/edited

With a function we will return the results within a WordPress shortcode.

///////////////////////////////////// Shortcode show Count Modified 48 Hours //////////////
add_shortcode( 'addocountpost', 'addo_count_posttype_modified' );
function addo_count_posttype_modified(){ 
	$args = array(
		'post_type' => 'crasyphotosgalleries',
		'date_query' => array(
			'relation'   => 'OR',
			array(
				'column'  => 'post_date',
				'after'   => '-2 days'
			),
			array(
				'column'  => 'post_modified',
				'after'   => '-2 days'
			)
		)
	);
	// query
	$updated = new WP_Query($args);
	$totalpost = $updated->found_posts; 
        return '<div class="addo40px addocenter">' . $totalpost . '</div>'; 
}

You can use the following shortcode in your content:

[addocountpost]

You can use the following shortcode in your templates:

<?php echo do_shortcode( '[addocountpost]' ); ?>

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>