Wordpress Popular Post counter

PHOTO EMBED

Fri Aug 23 2024 11:38:14 GMT+0000 (Coordinated Universal Time)

Saved by @andersdeleuran #php

<?php 
// Put this in Functions
function popular_posts($post_id) {
	$count_key = 'popular_posts';
	$count = get_post_meta($post_id, $count_key, true);
	if ($count == '') {
		$count = 0;
		delete_post_meta($post_id, $count_key);
		add_post_meta($post_id, $count_key, '0');
	} else {
		$count++;
		update_post_meta($post_id, $count_key, $count);
	}
}
function track_posts($post_id) {
	if (!is_single()) return;
	if (empty($post_id)) {
		global $post;
		$post_id = $post->ID;
	}
	popular_posts($post_id);
}
add_action('wp_head', 'track_posts');
?>
  
 <h3>Popular Posts</h3>
<ul>
	<?php 
		$args_mostread = array(
		'post_type' => 'nyheder',
		'meta_key'=>'popular_posts',
		'orderby'=>'meta_value_num',
		'posts_per_page' => 6,
		'order' => 'DESC',
		'category_name' => 'ligaherrer'
		);
	$mostread = new WP_Query( $args_mostread ); 
	while ($mostread->have_posts()) : $mostread->the_post(); ?>
	<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
	<?php endwhile; wp_reset_postdata(); ?>
</ul>


content_copyCOPY

Wordpress, PHP, popular posts

https://digwp.com/2016/03/diy-popular-posts/