Ask your WordPress questions! Pay money and get answers fast! (more info)

Display number of new post with certain tag WordPress

  • SOLVED

Hello i want to display the number of new posts that have been published in the last 24 hours for specific tags on my site.

See example below:

10 new posts with tag 'song" were published today so far. I would like a template tag where i can output the number of post for today and if there isn't any new post then it would output # of posts yesterday.

EXAMPLE: New Songs (10 Today) if no post published with tag in last 24 hours then display New Songs (12 yesterday)

Please see picture for example. (At very top of page)

Answers (3)

2014-03-31

Luis Abarca answers:

Try this


<?php
function posts_per_day($tag_slug)
{
// Posts are within the 24 hours
$posts_day = 'today';

// Get the last 24 hrs posts
$myquery = new WP_Query(array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'nopaging' => true,
'tag' => $tag_slug,
'date_query' => array(
array(
'after' => '1 day ago'
)
)
));

if ($myquery->post_count < 1) {
// Posts are from yesterday
$posts_day = 'yesterday';

// Get the today posts
$myquery = new WP_Query(array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'nopaging' => true,
'tag' => $tag_slug,
'date_query' => array(
array(
'after' => '2 days ago'
)
)
));
}

printf("Posts (%d %s)", $myquery->post_count, $posts_day);
}

posts_per_day('songs');


Luis Abarca comments:

Try this better version with the tag name on it


<?php
function posts_per_day($tag_slug, $taxonomy = 'post_tag')
{
// Posts are within the 24 hours
$posts_day = 'today';

// Get the last 24 hrs posts
$myquery = new WP_Query(array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'nopaging' => true,
'tag' => $tag_slug,
'date_query' => array(
array(
'after' => '1 day ago'
)
)
));

if ($myquery->post_count < 1) {
// Posts are from yesterday
$posts_day = 'yesterday';

// Get the today posts
$myquery = new WP_Query(array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'nopaging' => true,
'tag' => $tag_slug,
'date_query' => array(
array(
'after' => '2 days ago'
)
)
));
}

$term = get_term_by('slug', $tag_slug, $taxonomy);

return sprintf("%s (%d %s)", $term->name, $myquery->post_count, $posts_day);
}

echo posts_per_day('songs');


George Sprouse comments:

Hey Luis, that worked great! If i wanted to change it to 12 hours would i change the "today" array for post_day?

Thanks so much! Saved me a couple headaches..appreciate it


Luis Abarca comments:

Hi George,

Is good tyo hear it, you can use any kind of format supported by [[LINK href="http://php.net/strtotime"]]strtotime[[/LINK]]

http://php.net/manual/en/datetime.formats.relative.php

Change the next section on your current query.
'after' => '-12 hours'


if ($myquery->post_count < 1) {
// Posts published with in 12 hrs ago (this is justa a label)
$posts_day = '12 hours ago';

// Get posts published 12 hours ago
$myquery = new WP_Query(array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'nopaging' => true,
'tag' => $tag_slug,
'date_query' => array(
array(
<strong>'after' => '-12 hours'</strong>
)
)
));
}

2014-03-31

Arnav Joy answers:

try this
<?php
$counter = 0;
query_posts( 'tag=custom' );

if ( have_posts() ) while ( have_posts() ) : the_post();
$counter++;

endwhile;endif;

echo "Post count are=".$counter;

?>

2014-03-31

Dbranes answers:

I wonder if you mean something like this (tested):


/**
* Count posts with a given tag and date
*
* @uses WP_Query()
* @param string $tag Tag slug
* @param string $after Date after
*/
function wpq_count( $tag = '', $after = '1 day ago' ){

$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'fields' => 'ids', // Fetch only the ID's, not the whole post content
'tax_query' => array(
array(
'taxonomy' => 'post_tag', // Tags
'field' => 'slug',
'terms' => $tag
)
),
'date_query' => array(
array(
'after' => $after ,
),
),
);
$q = new WP_Query( $args );
return $q->found_posts;
}



where you can use it like:

printf( 'Videos (%d) today', wpq_count( 'videos', '1 day ago' ) );


or wrap it into your own custom template tag:


/**
* Custom template tag to show tag count:
*
* Example: the_count( 'videos', 'Videos' );
*
* @uses wpq_count()
* @param string $tag Tag slug
* @param string $label Label
*/
if( ! function_exists( 'the_count' ) && function_exists( 'wpq_count' ) ):

function the_count( $tag = '', $label = '' ){
if( ( $count = wpq_count( $tag, '1 day ago' ) ) > 0 ) {
$ago = 'today';
} elseif( ( $count = wpq_count( $tag, '2 days ago' ) ) > 0 ){
$ago = 'yesterday';
}
printf( '%s ( %d %s )', $label, $count, $ago );
}

endif;


---

But it might make your life easier to construct a more general template function:

if( ! function_exists( 'the_countx' ) && function_exists( 'wpq_count' ) ):

function the_countx( $args = array() ){

$default = array( 'tag' => '', 'label1' => '', 'after' => '0 day ago', 'label2' => 'today' );

foreach( $args as $a ){

$a = wp_parse_args( $a, $default );

if( ( $count = wpq_count( $a['tag'], $a['after'] ) ) > 0 ) {
printf( '%s ( %d %s )', $a['label1'], $count, $a['label2'] );
break;
}
}
}

endif;


with the following usage:

- Only a single check:

the_countx( array(
array( 'tag' => 'videos', 'label1' => 'Videos', 'after' => '1 day ago', 'label2' => 'today' )
));



- Double check:

the_countx( array(
array( 'tag' => 'videos', 'label1' => 'Videos', 'after' => '0 day ago', 'label2' => 'today' ),
array( 'tag' => 'videos', 'label1' => 'Videos', 'after' => '1 day ago', 'label2' => 'yesterday' ),
));


and so on.

---

You can modify this further to your needs, but you get the idea,

( you might also consider caching it )


Hope this help.