I am looking to display posts that meet certain requirements. First and foremost, the idea is to show visitors the most viewed posts. I implemented that with some help. So, I got that working with the following PHP.
I got that working for me, which is nice. Now I want to add a constraint, namely: only posts with thumbnails should be eligible for the loop, but you can't just skip a post if it doesn't have a thumbnail and just come up with less posts in the actual output: I need four posts in total! So, ideally I want a way to pass an argument that says "has thumbnail" to the WP_Query. However, I also need the meta_key already present. How can I keep that meta key, and use another: I found here that you can add 'key' => '_thumbnail_id'
, but I'm not sure how I can add multiple keys into this query.
Keep in mind that I do need to use orderby
, which orders the posts in order of most views.
For completeness sake, I will post the whole snippet, with Pieter Goosen's answer in mind. I thought the "30 day" would not make a difference, but now I realise that it might. post_date_selection
is a function in functions.php
that restricts the posts to the last 30 days. I got it from here.
<?php if (is_home()) : ?>
<section class="featured-posts clear">
<?php // restrict loop to 30 last days, see functions.php
add_filter('posts_where', 'post_date_selection');
global $query_string;
query_posts($query_string);
?>
<?php
$args = array(
'order' => 'DESC',
'posts_per_page' => 4,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'post_views_count',
'compare' => 'EXISTS'
),
array(
'key' => '_thumbnail_id',
'compare' => 'EXISTS'
),
),
);
$popularPosts = new WP_Query($args);
$counter = 1;
?>
<?php while ($popularPosts->have_posts() ) : $popularPosts->the_post(); ?>
<article class="box-<?php echo $counter++; ?>">
<a href=" <?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?><span><?php the_title(); ?></span></a>
</article>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
</section>
<?php endif; // is_home ?>
<?php remove_filter('posts_where', 'post_date_selection'); // remove restriction from loop ?>
Most important functions in functions.php:
/*
* Getting and setting post count
*/
// Set post count
function set_post_views($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
// To keep the count accurate, lets get rid of prefetching
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
// Update post count
function track_post_views ($post_id) {
if ( !is_single() ) return;
if ( empty ( $post_id) ) {
global $post;
$post_id = $post->ID;
}
set_post_views($post_id);
}
add_action( 'wp_head', 'track_post_views');
// Only 30 last days, needs in content
function post_date_selection ($when = '') {
$when .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $when;
}