X

Comment afficher les articles les plus populaires à l’aide des métas Views Post

Snippets by IsItWP

Vous cherchez un moyen d’afficher les articles les plus populaires en utilisant les vues post meta ? Bien qu’il existe probablement un plugin pour cela, nous avons créé un extrait de code rapide que vous pouvez utiliser pour afficher les articles les plus populaires en utilisant les vues post meta dans WordPress.

Vous pouvez également consulter comment suivre les affichages d’articles sans plugin en utilisant les méta-pages.

Instructions:

Tout ce que vous avez à faire est d’ajouter ce code au fichier index.php de votre thème :

<? 
query_posts('meta_key=post_views_count&orderby=meta_value_num&order=DESC'); 
?>

Note : Si c’est la première fois que vous ajoutez des extraits de code dans WordPress, veuillez vous référer à notre guide sur la façon de copier/coller correctement des extraits de code dans WordPress, afin de ne pas casser accidentellement votre site.

Si vous avez aimé cet extrait de code, n’hésitez pas à consulter nos autres articles sur le site comme : Les 10 meilleurs hébergeurs et Comment choisir le meilleur registrar de nom de domaine.

Commentaires   laisser une réponse

  1. Thank you for the tips

  2. Thanks for your tips! Very nice

  3. Felipe Brandão août 25, 2013 à 8:58 pm

    Hey Kevin, how do I display only the most popular from “today”?

  4. Thanks butt i can’t use this tips.please help me simple touch.Thanks to bro

  5. How to display popular post related to specific category on post-pages?? And how to arrange that widget if we add using functions.php??

    1. Just add &cat= after DESC and enter the id number for the category.

  6. this is amazing, thanks so much… i’m having problems with the pages navigation (next_posts_link/previous_posts_link) it shows the right amount of posts but the same post in every page. how can i fix this?? please help!
    thanx!

    1.  Its hard to say without seeing the code involved. I would suggest sending me an email and ill have a quick look. http://wpsnipp.com/index.php/contact/

      1. Were you ever able to resolve the issue with this user. I am having the same problem in my popular posts page. All the paginated pages show the same popular posts from page one.

  7. Display Popular Posts in WordPress to Keep Your Best Content Fresh mars 13, 2012 à 10:53 am

    […] We have a nice little tutorial here on How to Display Your Most Popular Posts in the Last 30 Days. WPSnipp.com also have a good article on displaying popular posts using post views data. […]

  8. i’m getting two post only in result. though I have pass posts_per_page=5 to query post. 

    below is my loop. Can you please let me know where I’m going wrong?

    query_posts(‘meta_key=views&orderby=views&order=DESC&posts_per_page=5”)

    I have tried you query post as well but I got same result. Only two post in output.

    1. that query looks fine, I would of course assume that you have more than 2 posts. Could be some other code within your template.

    2. This is because only two of your posts have been viewed and actually have a custom field titled “views” created. I had the same problem. Once a post was viewed for the first time, it appeared on the category page.

  9. Rafael Apocalypse septembre 1, 2011 à 3:49 pm

    I’ve tried this code to use in a website I’m building, but for some strange reason it was updating 2 posts, always, and kind of aleatory, it was always the same, but in a few tries, it changed, I don’t really know why.

    Searching online, I’ve found somebody using global $post; and $post_id = (int)$post->ID; to get the post ID for use on the update_post_meta(); So I changed my code, and everything is ok now.

    1. Hi Rafael, Interesting hhhmmmm, a few people also said they had issues I wonder if this would fix it. I will test things out and make an update. Thanks for the info….

  10.  Hi, I tried to integrate this snippet on my page, but it only works half the way it should do. Everytime a post is clicked, the code snippet also displays the post after this one as viewed by someone. Hope you understand what i mean 😉

    1. Well this snippet works with the following snippet to save the views into postmeta,  http://wpsnipp.com/index.php/functions-php/track-post-views-without-a-plugin-using-post-meta/

      So if you have both setup it should work fine.  The snippet above just lets you order the posts by the post views if you have both snippets setup.

      1.  Thank you for your reply. (Unfortunately) I have both setup and it doesn’t work. It’s pretty strange to me…

        1. what version of wordpress are you currently running I have tested it in 3.1.2 fresh install. It could be one of your plugins that is causing problems.

        2. what version of wordpress are you currently running I have tested it in 3.1.2 fresh install. It could be one of your plugins that is causing problems.

        3. I am running 3.1.1. But I don’t think there is a significent difference between them two, isn’t it?

        4. No 3.1.1 should not be an issue, what might cause an issues is a plugin

  11. Fixing my own problem:
    orderby=post_views_count must be changed to orderby=meta_value_num meta_value_num was added to the core a while ago: http://core.trac.wordpress.org/ticket/10649Robert

    1. Ahh ok cool glad you got things working. Can you post a new link to the bug – I get a not found error.

      1. Actually he`s right. I got only random posts using this code and changing the orderby to meta_value_num actually fixed the problem. Thanks

        1. Daniel you are correct, did some testing have just updated the post, thanks again for the input.

  12. The sorting doesn’t work correctly. My posts ordered like this: 9,8,7,6,5,4,3,2,1,10,11,12 etc.

    Any idea to get the right order: 12,11,10,9,8 …?

  13. simone bernacchia avril 22, 2011 à 1:04 am

    With a little modification can also work with wp-comments, since it uses the same method:

    query_posts('meta_key=views&orderby=views&order=DESC');

    1. I’m not familiar with wp-comments is this a plugin?

  14. Balkanboy1982 mars 28, 2011 à 4:40 pm

    How do you set up this to show most popular posts for today, this week or this month?

    This is all time I presume.

    Thanks.

    1. You can just add a little to the query_post &monthnum=’ . date( ‘n’, current_time( ‘timestamp’ ) )

      query_posts(‘meta_key=post_views_count&orderby=post_views_count&order=DESC&monthnum=’ . date( ‘n’, current_time(‘timestamp’))’);

      1. dear Kevin i tried these but it’s not working here is my code 

        plz help me

      2. these but it’s not working here is my code 
        query_posts(‘meta_key=post_views_count&orderby=post_views_count&order=DESC&monthnum=’ . date( ‘n’, current_time(‘timestamp’))); if ( have_posts() ) : while ( have_posts() ) : the_post(); 
        dear Kevin i tried 

        plz help me

        1.  your orderby value is not correct,

          1. please reply to Yashmistrey Kevin !!

          2. Sangeeta, I did, in above snippet you can see

            orderby=meta_value_num

    2. Balkanboy1982 mars 28, 2011 à 5:01 pm

      Thanks

  15. 20 More WordPress Code Snippets and Hacks mars 28, 2011 à 5:27 am

    […] <? query_posts('meta_key=post_views_count&orderby=post_views_count&order=DESC'); ?> Source →Breadcrumbs Without a PluginBreadcrumbs can be a useful navigation technique that offers link to the […]

  16. Got it!

    Here’s my code.

    <a href="”>

    1. Cool, glad you got things working, this is a link to some sample code you could use as well,
      http://wpsnipp.com/wp-content/uploads/2011/03/post-views-per-page.txt

  17. I cant get this working

    Like this?

    ‘dropzone’, ‘posts_per_page’ => 2) ); ?>

    have_posts() ) : $loop->the_post(); ?>
    <a href="”>

    1. Hi Alec,
      If you want to limit the number of posts you would do this
      http://wpsnipp.com/wp-content/uploads/2011/03/posts-per-page-post-views.txt

Ajouter un commentaire

Nous sommes heureux que vous ayez choisi de laisser un commentaire. N'oubliez pas que tous les commentaires sont modérés conformément à notre privacy policy, et que tous les liens sont en nofollow. N'utilisez PAS de mots-clés dans le champ du nom. Engageons une conversation personnelle et constructive.

WordPress Launch Checklist

L'ultime liste de contrôle pour le lancement de WordPress

Nous avons rassemblé tous les éléments essentiels de la liste de contrôle pour le lancement de votre prochain site Web WordPress dans un ebook pratique.
Oui, envoyez-moi le gratuit !