Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Im using following code for getting most populer post with thumbnail.I have a custom post type taxonomy (Talks). Custom post types(video) have 6 category.

But my code not working. It just getting 1 post.But i have 10 post. How can i fix it?

    <?php

$args = array(
    'post_type' => 'video',
    'showposts' => 10,
    'order' => 'DESC',
    'meta_key' => 'post_views_count',
    'orderby' => 'meta_value_num',

    'tax_query' => array(
        array(
            'taxonomy' => 'Talks',
            'field' => 'id',
            'terms' => array(
                371,
                372,
                373,
                374,
                375,
                376
            )
        )
    )
); // 
$loop = new WP_Query($args);
while ($loop->have_posts()):
    $loop->the_post();
?>


                              <div class="box">
                               <a class="box-over" href="<?php
    the_permalink();
?>">
                                                <?php
    if (($video_thumbnail = get_video_thumbnail()) != null) {
        echo "<img src='" . $video_thumbnail . "' class='videowisetalksthumb' width='150' height='108' />";
    }
?>
                               </a>

                               <p class="videotitle"><a href="<?php
    the_permalink();
?>"><?php
    the_title();
?></a></p>        

                              </div>

                              <?php
endwhile;
?>
share|improve this question
    
First of all, you probably want to order by DESC, since you want the most popular on top, but that's an aside. How is post_views_count populated? Do you have more than one post with the post_views_count postmeta set? Note that this query will ONLY get posts if that is set. –  Matthew Boynes Mar 7 '13 at 20:57
    
'post_views_count' maybe not populated but i need most viewed post.This value is important for me. How can list most viewed posts? Do you have any idea? I said, my code just listing one post. Thank for answer @Matthew Boynes –  Genxer Mar 7 '13 at 21:07
    
If you don't have a plugin currently populating the view count data, you'll need one: wordpress.org/extend/plugins/search.php?q=popular. Best of luck! –  Matthew Boynes Mar 7 '13 at 21:11
    
I need do it without plugins. Thanks @Matthew Boynes –  Genxer Mar 7 '13 at 21:29

2 Answers 2

up vote 0 down vote accepted

Brain helped me..

I have installed already WP-PostViews Plugin

I just changed following line on my code and it worked.

Thanks for all answers and suggestion.

'meta_key' => 'views'

share|improve this answer

If you can't use an existing plugin, then you could do something like this: Post views without plugin.

In the linked example, the author calls setPostViews() from single.php, but if you have a custom single template for your custom post type, then you'd add it there instead. This will increment your post view counts, which you can then query.

One last note: make sure to call wp_reset_postdata() after your display loop since you looped through a custom WP_Query. This is especially important if the code above is being rendered as a widget of some sort.

share|improve this answer
    
Hello @Brian, I think I explained it wrong. I do not need just the number of views.If you reviewed my code, I have already listed just one post? but why? I'm looking for an answer to it. Thanks for all! –  Genxer Mar 7 '13 at 22:14
    
There is a missing comma after 'order' => 'DESC' that would affect the $args passed to WP_Query. You might also verify that the terms specified in 'tax_query' are not inadvertently filtering your results -- try removing the 'tax_query' and seeing if that increases the number of results you get. –  Brian Mar 7 '13 at 22:30
    
Hello Brian, I made ​​a mistake while editing code (a missing comma after 'order' => 'DESC') thanks, but still just one post listing ( 'tax_query' removed ). –  Genxer Mar 7 '13 at 22:38
    
All I can guess is that there is only a single post that has a custom key of 'post_views_count', or that there is only a single post matching that 'post_type'. Try removing the 'meta_key' filter first, and if that doesn't work, remove 'post_type'...working your way back to super simple to see where the query is breaking down. –  Brian Mar 7 '13 at 22:50
    
When i remove 'meta_key' it worked but listing recent post. –  Genxer Mar 7 '13 at 22:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.