mkafiyan
mkafiyan

Reputation: 954

display WordPress popular post without using plugins for specific custom post

Actually I want to find most popular post in special custom post type. I'm completely new to WordPress and try to create my own theme. I have custom post type name book and I want to find most popular book. First I added WordPress most popular plugin but I don't know how can I use it in my code and when I searched I can't find any way to call it inside of codes so try to go to next step and write it myself. I search and find this question here. Good way but I want to use this for specific custom post not all post.

Here is my code but it work for all post not one custom post type

 function getPostViews($postID){
     $count_key = 'post_views_count';
     $count = get_post_meta($postID, $count_key, true);
     if ($count == '') {
         delete_post_meta($postID, $count_key);
         add_post_meta($postID, $count_key, '0');

         return "0";
     }
     return $count;
}

function setPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if ($count== '' ) {
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    } else {
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}

here in my html

                                    <?php $populerbook= new WP_Query(
                                    array('posts_per_page'=> 6,
                                          'post_type' => 'book',
                                          'meta_key' => 'post_views_count',
                                          'orderby' => 'meta_value_num',
                                          'order' => 'DESC')
                                );
                                while ($populerbook->have_posts()): $populerbook->the_post();

                                ?>
                                        <a href="<?php post_permalink(); ?>">
                                            <?php the_post_thumbnail(); ?>
                                                <h4><?php the_title(); ?></h4>
                                                <?php
                                                    if ( get_field('cost') ) {  
                                                echo '<p>'.get_field('cost').'تومان</p>';
                                            } ?>

                                        </a>
                                    <?php endwhile ?>

can any one help me with this issue ?

Upvotes: 0

Views: 1452

Answers (1)

Sam Craddock
Sam Craddock

Reputation: 108

I threw this together. Hopefully this can help you or someone else answer this problem.

This should create an array of all the custom posts of the type "book".

$args = array(
        'post_status' => 'publish',
        'post_type' => 'book',
        'meta_key' => 'post_views_count',
        'orderby' => 'meta_value_num',
        'order' => 'DESC'
    );

$posts_array = get_posts( $args );

Then we loop through the array and display each value of the post as required. In this example we output the title,author and views of each post.

foreach( $posts_array as $post => $value){

 $post_views = $value->post_views_count;
 $post_title = $value->post_title;
 $post_author = $value->post_author;

 echo $post_title;
 echo $post_author;
 echo $post_views;

}

Addition

   <?php 
        $args = array('posts_per_page'=> 6,
                       'post_type' => 'book',
                       'meta_key' => 'post_views_count',
                       'orderby' => 'meta_value_num',
                       'order' => 'DESC'
                       );
        $populerbook = new WP_Query( $args );

I added this print_r() to display what you are outputting.

         echo "<pre>";
         print_r($populerbook);
         echo "</pre>";

This should show the contents of $populerbook. Once you have this information you can work out where it is going wrong. So either you are not getting the data or not outputting correctly (this may be the case if it's a multidimensional array).

        while ($populerbook->have_posts()): $populerbook->the_post();

      ?>

      <a href="<?php post_permalink(); ?>">
          <?php the_post_thumbnail(); ?>
          <h4><?php the_title(); ?></h4>

          <?php if ( get_field('cost') ) {  
             echo '<p>'.get_field('cost').'تومان</p>';
           };?>

        </a>
       <?php endwhile ?>

Upvotes: 1

Related Questions