Marco Romano
Marco Romano

Reputation: 456

wp_query for show the post must view and with shortcode

I have try to use this function for show the post most view. But something it's wrong.

I use this also for other things, like show the post by category and in this case work well.

So maybe I have wrong something in the array of new WP_query

function must_post_read()
{
    global $post;
    $html = "";

    $my_query_2 = new WP_Query(array(
        'meta_key' => 'post_views_count',
        'orderby' => 'meta_value_num',
        'posts_per_page' => 5
    ));

    if ($my_query_2->have_posts()) : 
        while ($my_query_2->have_posts()) : $my_query_2->the_post();

            $html .= "<p class=\"title\">" . get_the_title() . " </p>";
            $html .= "<p>" . get_the_excerpt() . "</p>";
            $html .= "<a href=\"" . get_permalink() . "\" class=\"readmore\">Read more</a>";

        endwhile;
        wp_reset_postdata();
    endif;
    return $html;
}

add_shortcode('must_post', 'must_post_read');

Someone can help me please?

Upvotes: 0

Views: 76

Answers (1)

Raunak Gupta
Raunak Gupta

Reputation: 10809

First we need to make sure post has a custom post_meta fields on which you 'll be doing your shorting.

function whpp_track_post_views($post_id) {
    if (!is_single())
        return;
    if (empty($post_id)) {
        global $post;
        $post_id = $post->ID;
    }
    whpp_set_post_views($post_id);
}

add_action('wp_head', 'whpp_track_post_views');

function whpp_set_post_views($post_id) {
    $count_key = 'whpp_track_post_views';
    $count = get_post_meta($post_id, $count_key, TRUE);
    if ($count == '') {
        $count = 0;
        delete_post_meta($post_id, $count_key);
        add_post_meta($post_id, $count_key, '0');
    } else {
        $count++;
        update_post_meta($post_id, $count_key, $count);
    }
}

//To keep the count accurate, lets get rid of prefetching
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

//Short code function
function wh_must_post_read() {
    $html = '';

    $args = [
        'meta_key' => 'whpp_track_post_views', //<--  Check This
        'orderby' => 'meta_value_num',
        'order' => 'DESC',
        'posts_per_page' => 5
    ];
    
    $my_query_2 = new WP_Query($args);

    if ($my_query_2->have_posts()) :
        while ($my_query_2->have_posts()) : $my_query_2->the_post();

            $html .= "<p class=\"title\">" . get_the_title() . " </p>";
            $html .= "<p>" . get_the_excerpt() . "</p>";
            $html .= "<a href=\"" . get_permalink() . "\" class=\"readmore\">Read more</a>";

        endwhile;
        wp_reset_postdata();
    endif;
    return $html;
}

add_shortcode('wh_must_post', 'wh_must_post_read');

USAGE
In PHP

echo do_shortcode('[wh_must_post]');

In WP Editor

[wh_must_post]

Please Note: To see this code in action visit few post single page so that whpp_set_post_views() mehord will add whpp_track_post_views meta_key to that post.

All the above code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.
Code is tested and works.

Related Question : How to change the order of posts by number of views not by date in wordpress

Upvotes: 1

Related Questions