Files
agroexpert/partials/most-read-posts.php
2024-03-01 17:47:03 +03:00

32 lines
1.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
global $wpdb; // Глобальная переменная для работы с базой данных
// Запрос для получения ID топ-3 просматриваемых постов
$query = "
SELECT p.ID, p.post_title, pv.count
FROM {$wpdb->prefix}posts p
JOIN {$wpdb->prefix}post_views pv ON p.ID = pv.id AND pv.type = 4
WHERE p.post_status = 'publish' AND p.post_type = 'post'
ORDER BY pv.count DESC
LIMIT 3;
";
$top_posts = $wpdb->get_results($query);
// Вывод результатов
if (!empty($top_posts)) {
foreach ($top_posts as $post) {
$permalink = get_permalink($post->ID);
$title = get_the_title($post->ID);
?>
<div class="most-read__link">
<a href="<?php echo esc_url($permalink); ?>" class="most-read__link-inner text-13 link-icon-after">
<?php echo esc_html($title); ?>
</a>
</div>
<?php
}
} else {
echo '<p>Нет просмотренных постов.</p>';
}
?>