M
M
Mauster2016-01-30 09:52:38
css
Mauster, 2016-01-30 09:52:38

How to display the number of posts in a category in the last 24 hours in WordPress?

Hello! How to display next to the rubric the number of added entries in 24 hours, for example?
I use the wp_list_categories() function, but using this function, you can only display all the posts of the category. Is it possible to implement this, if so, how? Thanks in advance for your reply.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Tretyakov, 2016-01-30
@Mauster

// Получаем время отсчета. То есть текущее время минус 24 часа = точка старта.
$day = date('Y-m-d H:i:s', strtotime('-1 day'));

// Делаем запрос к бд, получая в ответ все записи, которые
// были опубликованы за последние 24 часа
$numpost = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND post_date > '$day'");

// Выводим результат 
echo $numpost;

I didn't read carefully. It appears it is necessary for each heading.
Therefore, I add code that displays for categories:
$this_cat = get_the_category();
  
$this_cat = $this_cat[0]->cat_ID; // id текущей рубрики

$day = date('Y-m-d H:i:s', strtotime('-1 day'));

$numpost = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts 
              WHERE post_status = 'publish' AND post_type = 'post' AND post_date > '$day' AND 
              id IN (SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = '$this_cat')");

echo $numpost;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question