• Hello sir
    I’m creating a list in my theme and I will get popular post by category Id in this list
    How to do this?

Viewing 1 replies (of 1 total)
  • Moderator t-p

    (@t-p)

    Try using this WP_Query or get_posts:

    <?php
    $popularpost  = new WP_Query( array(
        'cat'=> 2,
        'posts_per_page' => 1,
        'meta_key' => 'post_views_count',
        'orderby' => 'meta_value_num',
        'order' => 'DESC'
    ) );
    
    if ($popularpost->have_posts()) {
        while ($popularpost->have_posts()) {
            $popularpost->the_post(); ?>
            <li><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
            <?php
        }
        wp_reset_postdata();
    }
    ?>
Viewing 1 replies (of 1 total)
  • The topic ‘How to get popular post by category’ is closed to new replies.