I’m having some difficulty getting a loop that gives me post by most viewed.
I started by following this tutorial
This worked fine. How ever I have some 700 posts that where added with php script and json from a different site. In the JSON I had some extra information that I added as meta tags (custom fields). One of the information I got from the JSON where the current view count. I then have a work around that calculates new views along with the views from the other website(it’s stored on both sites for a while, long story)
Adding the view count to the posts is no a problem and works fine.
But when I finally get to querying the posts I get a bug. The query only shows me posts with view count under thousand, all post with view count over 1000 are not included. There are over 100 posts with view count over 1000.
My query is so:
$popularpost = new WP_Query( array( 'posts_per_page' => 20, 'meta_key' => 'viewCount', 'orderby' => 'viewCount', 'order' => 'DESC' ) );
while ( $popularpost->have_posts() ) : $popularpost->the_post();
include('content-markup.php');
endwhile;
I might point out that it is being done in a custom template page, and the included php file is includes the markup I use to style the post. If I remove the included file and use the_title(); instead it make no different, I just get the titles for the same posts.
The first post has 999 views, the next after it has 1012 views.
For some reason something is limiting the query so that it can only show post with the meta value under 1000.
I’m not sure what else I can inform you off, I hope that someone out there can help me out (and someone else can correct my grammar) 😀
The problem is in the orderby it has to be
And don’t forget
wp_reset_postdata()
at the end of the loop