Task:7037 | Вывел самые читаемые посты по категориям

pull/36/head
parent 0b359d7a1c
commit 48678b81ce
  1. 39
      wp-content/themes/cosmopet/modules/blog/components/most-read/component-controller.php

@ -1,22 +1,43 @@
<?php <?php
add_filter('timber/context', function($context) { add_filter('timber/context', function($context) {
// Простой запрос для получения последних постов // Получаем текущую категорию
$current_category = get_queried_object();
$category_id = null;
if (isset($current_category) && !is_wp_error($current_category) && !empty($current_category->term_id)) {
$category_id = $current_category->term_id;
$category_name = $current_category->name;
} else {
$category_name = null;
}
// Формируем аргументы для запроса
$args = array( $args = array(
'post_type' => 'post', 'post_type' => 'post',
'posts_per_page' => 3, 'posts_per_page' => 3,
'orderby' => 'date', 'meta_key' => 'post_views_count', // Предполагаемое мета-поле для просмотров
'order' => 'DESC' 'orderby' => 'meta_value_num', // Сортировка по числовому значению мета-поля
'order' => 'DESC',
); );
// Если выбрана категория, добавляем фильтр по ней
if ($category_id) {
$args['cat'] = $category_id;
}
// Выполняем запрос
$most_read_query = new WP_Query($args); $most_read_query = new WP_Query($args);
$most_read = new Timber\PostQuery($most_read_query); $most_read = new Timber\PostQuery($most_read_query);
// Добавляем данные в контекст
$context['most_read'] = $most_read; $context['most_read'] = $most_read;
$context['current_category'] = $category_name;
// Добавляем отладочную информацию $context['category_list'] = get_categories();
// Отладочная информация (опционально)
global $wpdb; global $wpdb;
$debug_info = $wpdb->get_results("SELECT post_id, meta_key, meta_value FROM {$wpdb->postmeta} WHERE meta_key LIKE '%view%' LIMIT 5"); $debug_info = $wpdb->get_results("SELECT post_id, meta_key, meta_value FROM {$wpdb->postmeta} WHERE meta_key = 'post_views_count' LIMIT 5");
$context['debug_info'] = $debug_info; $context['debug_info'] = $debug_info;
return $context; return $context;
}); });
Loading…
Cancel
Save