Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

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

  • SOLVED

Hi,

I'm using this plugin [[LINK href="http://wordpress.org/extend/plugins/wp-postviews/"]]WP-PostViews[[/LINK]] 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 <strong>veiws</strong>

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

Answers (3)

2011-12-14

Luis Abarca answers:


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


Or:

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


Josh Cranwell comments:

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 <strong>'post_type' => 'any'</strong> and that breaks the loop. But manoj says the query works.

How weird is this.

2011-12-15

Manoj Raj answers:

Hi,

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

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


Manoj Raj comments:

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,

<strong>Count Views From:</strong> 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.


Josh Cranwell comments:

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


Manoj Raj comments:

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?


Josh Cranwell comments:

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


Josh Cranwell comments:

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 :/


Manoj Raj comments:

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


Josh Cranwell comments:

I'll DM you... thanks


Manoj Raj comments:

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


Manoj Raj comments:

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


Manoj Raj comments:

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


Josh Cranwell comments:

Yeh I did but still returning blank.

The loop works as doesn't break.

I forgot to send you the link.


Manoj Raj comments:

Its working now.. Can you check now?


Manoj Raj comments:

In Settings -> PostViews

<strong>Count Views From:</strong> option is set to <strong>Registered Users Only</strong>

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.


Josh Cranwell comments:

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


Manoj Raj comments:

Just try adding

'post_type' => 'any'

to check whether the views are working for pages?


Manoj Raj comments:

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


Josh Cranwell comments:

Its working!

<strong>'post_type' => 'page'</strong> and <strong>'post_type' => 'any'</strong> 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

2011-12-14

Navjot Singh answers:

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; ?>