简体   繁体   中英

How to display most viewed posts in custom post type

I'm using keremiya theme for wordpress. I was trying to display my most viewed post in my custom post type if "most_viewed" option is on. The name of my custom post type is watch. How can i do this with my current code? I am also using a plugin called wp-post views to display the views in my sidebar. Here is my query.

    <?php if(get_option('most_viewed') == 'On'): ?>
    <div class="sidebar-right">
    <h2><?php echo get_option('my_title'); ?></h2>
    <div class="fimanaortala">
    <?php $tavsayi = get_option('keremiya_tavsiyesayi'); $tavkat = get_option('keremiya_tavsiyekat');?>
    <?php query_posts('showposts='.$tavsayi.'&v_orderby=desc&cat='.$tavkat.'') ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div class="filmana">
        <div class="filmsol">
        <?php keremiya_resim('80px', '70px', 'izlenen-resim'); ?>
        </div>
        <div class="filmsag">
            <div class="filmsagbaslik">
            <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
            </div>
            <div class="filmsagicerik">
            <?php if(function_exists('the_views')) { the_views(); echo " "; } ?>
            <p><?php nezaman_yazildi(); ?></p>
            </div>
            <div class="filmizleme">
            <a href="<?php the_permalink() ?>"><img src="<?php bloginfo('template_directory'); ?>/images/filmizle.png" alt="film izle" height="21" width="61" /></a>
            </div>
        </div>
    </div>
    <?php endwhile; else: ?>
    <?php endif; ?>
    <?php wp_reset_query(); ?>
    </div>
</div>

Your solution (or attempt, at least), is based on a plugin called WP-PostViews, which I have no knowledge of. So, I can't really help you there. I can, however, help you solve it without this or any other plugin. So here we go:

Wordpress has a little something called metadata . From this very own link:

The Metadata API is a simple and standarized way for retrieving and manipulating metadata of various WordPress object types. Metadata for an object is a represented by a simple key-value pair. Objects may contain multiple metadata entries that share the same key and differ only in their value.

That means you can create metadata for your custom post type that contains how many times it has been seen. To do so, we create a function:

<?php
    function set_views($post_ID) {
        $key = 'views';
        $count = get_post_meta($post_ID, $key, true); //retrieves the count

        if($count == ''){ //check if the post has ever been seen

            //set count to 0
            $count = 0;

            //just in case
            delete_post_meta($post_ID, $key);

            //set number of views to zero
            add_post_meta($post_ID, $key, '0');

        } else{ //increment number of views
            $count++;
            update_post_meta($post_ID, $key, $count);
        }
    }

    //keeps the count accurate by removing prefetching
    remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
?>

This will, given the post ID, increment a counter every time a post is viewed. Of course, we have to call this function somewhere in our code so it actually runs. You can do this in two ways: You can either call this function on your single template (which I believe is called single-watch.php), or you can add a simple tracker. I favor the second option, as it keeps your single post loop cleaner. You can achieve such tracking this way:

<?php
    function track_custom_post_watch ($post_ID) {
        //you can use is_single here, to track all your posts. Here, we're traking custom post 'watch'
        if ( !is_singular( 'watch') ) return; 

        if ( empty ( $post_ID) ) {

            //gets the global post
            global $post; 

            //extracts the ID
            $post_ID = $post->ID;    
        }

        //calls our previously defined methos
        set_views($post_ID);
    }
    //adds the tracker to wp_head.
    add_action( 'wp_head', 'track_custom_post_watch');
?>

There you go. Now, WordPress checks if the user is visiting a page corresponding to a watch single post. If they are, it increments the counter.

The only thing left now is to query for the post with the highest number of views. This is easily achievable using WP_Query . When you create your loop, do something like this:

<?php
    $query = new WP_Query( array(
            'post_type'     => 'watch', //your post type
            'posts_per_page' => 1, 
            'meta_key'      => 'views', //the metakey previously defined
            'orderby'       => 'meta_value_num',
            'order'         => 'DESC'
        )
    );

    while ($query->have_posts()) {
        $query->the_post();
        //whatever code you want
    }
?>

I kept my answer to PHP, so you can adapt to your mark-up needs. I also assumed your post-type is indeed called watch . I hope all of this helps you. If you want to query your posts in a slightly different way, I suggest you read the WP_Query docs. Cheers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM