Plugin Author
Ajay
(@ajay)
Hi. Thank you for the great comments π
Top 10 doesn’t save the data as meta keys, but in separate tables.
If you post the code and what you’re trying to achieve, I could have a look and suggest changes.
Hi Ajay! Thanks for the quick response!
Here’s what I’m trying to achieve, optimally – basically the ability to sort posts by view count.
<?php
$args = array(
'post_type' => 'natural-remedy',
'taxonomy' => 'nat_rem_cat',
'term' => 'top-10-lists',
'order' => 'DESC',
'meta_key' => 'some_custom_field', //This would optimally be a meta_key value (e.g. tptn_visit_count) that can be then used to sort posts by "popularity"
'orderby' => 'meta_value',
);
$loop = new WP_Query( $args );
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
//Post code goes here.
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
[Moderator Note: Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]
Plugin Author
Ajay
(@ajay)
If I understand correctly, are you trying to fetch the top posts for the particular post type and taxonomy?
If so you can try this code and append the $args array.
$top_posts = tptn_pop_posts( 'posts_only=1&limit=100' );
$top_posts_array = wp_list_pluck( $top_posts, 'postnumber' );
$args = array(
'post_type' => 'natural-remedy',
'taxonomy' => 'nat_rem_cat',
'term' => 'top-10-lists',
'order' => 'DESC',
'orderby' => 'meta_value',
'post__in' => $top_posts_array,
);
Hi Ajay,
Thanks so much for the reply! What I’m trying to do is actually order the posts by the amount of views. So the posts with the most views appear first in the post list, and so on.
Plugin Author
Ajay
(@ajay)
Hi,
$top_posts = tptn_pop_posts( 'posts_only=1&limit=100' );
$top_posts_array = wp_list_pluck( $top_posts, 'postnumber' );
This piece of code fetches the top posts by views. the subsequent pieces of code passes these as an array using the post__in option.
Hi Ajay!
This is fantastic – thanks for explaining. That works perfectly! I’m using it to create sorting buttons on the site we’re developing (sort by “Most Recent”, “Most Viewed”, “Most Comments”). This is invaluable!
One thing I noticed though – the array only seems to bring up posts that have a view count – if there are any posts (e.g. brand new ones) that don’t have a view count, they don’t appear in the list. I can simply ask the client to make sure to manually enter in at least 1 for any new post, but is there any way to pull the posts that have “0” as their view count, or does the function only pull posts with a number greater than 0? Either way, no worries – I can have the client add them manually, like I said.
Thanks again! You’re the best!
Plugin Author
Ajay
(@ajay)
It just pulls all posts that are present in the tptn_posts table which would only be those that have at least a single view count.
For new posts, you can always add the 1 view count manually or just visit the post once after it is posted as guest (ideally another browser)
That makes complete sense. I’ve instructed the client to add the count manually or visit the post on another browser. Thanks again for the great support!
Plugin Author
Ajay
(@ajay)
You’re welcome. Do consider writing a good review of the plugin π
Just a not for anyone following this thread or who had a similar thing they want to do – if you want to utilize this and actually show the Top Posts in their proper order, just make sure in your Query you have the order_by set to post__in, as follows:
‘orderby’ => ‘post__in’
That will utilize the order of the posts pulled in via the function.
Plugin Author
Ajay
(@ajay)
Thanks for the tip. This is very useful!