logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$15
Meta key from WP-PostViews no displaying posts in WP Query?

Hi,

I'm using this plugin WP-PostViews to gather post/page views.

Then in my sidebar I've created a WP_Query to list four posts with the most views in descending order.

I looked in the WP-PostViews plugins php and i think the meta_key is veiws

So here's my WP_Query below...

	

<?php $viewed = new WP_Query(array(

'order' => 'DESC',

'orderby' => 'meta_value',

'meta_key' => 'views',

'posts_per_page' => 4

)); ?>

<?php if ( $viewed->have_posts()) : while ($viewed->have_posts()) : $viewed->the_post(); ?>

<li>

<a href="<?php the_permalink() ?>">

<?php the_post_thumbnail('thumbnail'); ?>

<?php the_title(); ?>

</a>

</li>

<?php endwhile; unset($viewed); endif; ?>




But I can't work out why it is not returning anything? The plugin is definitely switched on.

Can any help me figure out where I'm going wrong.

Thanks
Josh

This question has been answered.

Josh Cranwell | 12/14/11 at 2:10pm Edit


(3) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    12/14/11
    2:20pm
    Navjot Singh says:

    Why you are creating a separate query when the plugin offers an inbuilt function to do that? Use this code:

    <?php if (function_exists('get_most_viewed')): ?>
    <ul>
    <?php get_most_viewed(4); ?>
    </ul>
    <?php endif; ?>

    Previous versions of this answer: 12/14/11 at 2:18pm | 12/14/11 at 2:18pm | 12/14/11 at 2:20pm

  • avatar
    Last edited:
    12/14/11
    2:53pm
    Luis Abarca says:


    $viewed = new WP_Query(array(
    'post_type' => 'any',
    'order' => 'DESC',
    'orderby' => 'meta_value',
    'meta_key' => 'views',
    'posts_per_page' => 4
    ) );


    Or:

    <?php query_posts('v_sortby=views&v_orderby=desc') ?>

    Previous versions of this answer: 12/14/11 at 2:21pm | 12/14/11 at 2:53pm

    • 12/15/11 5:16am

      Josh Cranwell says:

      I've tried both, nothing seems to be working. The plugin is definately working as I dropped this on my page template

      <?php if(function_exists('the_views')) { the_views(); } ?>

      This is show a post counter.

      I've tried both...


      <?php query_posts('v_sortby=views&v_orderby=desc') ?>

      <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

      <li>

      <a href="<?php the_permalink() ?>">

      <?php the_post_thumbnail('thumbnail'); ?>

      <?php the_title(); ?>

      <?php if(function_exists('the_views')) { the_views(); } ?>

      </a>

      </li>

      <?php endwhile; endif; ?>

      <?php wp_reset_query(); ?>


      and


      <?php $viewed = new WP_Query(array(

      'order' => 'DESC',

      'orderby' => 'meta_value',

      'meta_key' => 'views',

      'posts_per_page' => 4

      )); ?>

      <?php if ($viewed->have_posts()) : while ($viewed->have_posts()) : $viewed->the_post(); ?>

      <li>

      <a href="<?php the_permalink() ?>">

      <?php the_post_thumbnail('thumbnail'); ?>

      <?php the_title(); ?>

      <?php if(function_exists('the_views')) { the_views(); } ?>

      </a>

      </li>

      <?php endwhile; unset($viewed); endif; ?>


      I tried 'post_type' => 'any' and that breaks the loop. But manoj says the query works.

      How weird is this.

  • avatar
    Last edited:
    12/14/11
    10:17pm
    Manoj Raj says:

    Hi,

    Your query works fine for me. I found nothing wrong.

    And navjot singh, It seems he want a thumbnail along with the post (Customized list) ordered by meta key "views".. So he wrote a new query.

    • 12/14/11 10:45pm

      Manoj Raj says:

      Your Query works perfectly fine for me...

      Add this

      <?php if(function_exists('the_views')) { the_views(); } ?>


      after
      <?php the_title(); ?>


      to check the number of views a post has.

      In the PostViews Options,

      Count Views From: is set to "guests only" by default.

      Unless or otherwise you open the post as a guest, the counter won't be set.(As a result, the query returns nothing). So you should log off from admin and view the posts as a guest so that the counter will be set.

    • 12/15/11 5:01am

      Josh Cranwell says:

      Hi Manoj,

      Thanks for taking the time to try it out your self.

      I still can't get my query to work, could you posibly paste the enitre loop thats working for you? There must be a mistake it mine.

      I put this on my pages...

      <?php if(function_exists('the_views')) { the_views(); } ?>
      ...and the counter is definately working.

      Really scratching my head on this one.

      Thanks

    • 12/15/11 5:16am

      Manoj Raj says:

      Hi Josh,

      I did no changes to the code you pasted here..

      Did you try pasting this code in a new page template to check whether it is working or not?

    • 12/15/11 5:19am

      Josh Cranwell says:

      Hmmm, I wonder if something is conflicting then. I might try and change the meta key in the plugin.

    • 12/15/11 5:26am

      Josh Cranwell says:

      Actually that's probably not a good idea, will probably end up breaking it. Yes i've tried putting that query in many places. Cant believe it works for you but not me :/

    • 12/15/11 5:29am

      Manoj Raj says:

      can you provide me the url you are working right now?

    • 12/15/11 5:30am

      Josh Cranwell says:

      I'll DM you... thanks

    • 12/15/11 5:31am

      Manoj Raj says:

      ya..okay..have to go to my workplace. Will reply you back in a hour.

    • 12/15/11 6:19am

      Manoj Raj says:

      have you tried inserting wp_reset_query before the whole bunch of code you pasted here?

    • 12/15/11 6:20am

      Manoj Raj says:

      have you tried inserting wp_reset_query before the whole bunch of code you pasted here?

    • 12/15/11 7:20am

      Josh Cranwell says:

      Yeh I did but still returning blank.

      The loop works as doesn't break.

      I forgot to send you the link.

    • 12/15/11 7:48am

      Manoj Raj says:

      Its working now.. Can you check now?

    • 12/15/11 8:02am

      Manoj Raj says:

      In Settings -> PostViews

      Count Views From: option is set to Registered Users Only

      I am the only registered user it seems.(You've created only one registered user).I viewed the posts and the counter is set. It is working.

      or Have you made any changes? I dint notice any significant changes in your template files.

    • 12/15/11 8:08am

      Josh Cranwell says:

      Oh I see the problem now.

      It's only retrieving post views for posts.

      I have only just added some post.

      I need it to just return pages only, and not posts.

      But the plugin says its counts posts/pages

    • 12/15/11 8:14am

      Manoj Raj says:

      Just try adding

      'post_type' => 'any'

      to check whether the views are working for pages?

    • 12/15/11 8:20am

      Manoj Raj says:

      I tested it in my own domain..The plugin is working for pages also. Just add 'post_type' => 'any'

    • 12/15/11 8:34am

      Josh Cranwell says:

      Its working!

      'post_type' => 'page' and 'post_type' => 'any' both work

      I do not know why that was happening.

      I swear that I tried all of this but I was getting no result - seems to be all good now.

      Thanks Manoj for your time!

      Josh

This question has expired.



Gabriel Reguly, Josh Cranwell voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.