List POSTS in Wordpress by views

Asked

Viewed 373 times

1

I need to make sure: <?php while ( have_posts() ) : the_post(); ?> list according to the amount of views (post_views_count), it’s like to list popular posts only it will enter the table mh_postmeta, and list according to what you have more views.

tabela wordpress de post meta

The staff uses the 'orderby' => 'comment_count', to list according to the comments, but I need it to look for the other table, however, this can only happen for a certain category. Let’s say you have 40 CAT it should only list by VIEW instead of DATE in CAT 3.

2 answers

4


You have to make a query a little more complex, which will lead you to seek a solution beyond the loop principal (i.e., <?php while ( have_posts() ) : the_post() ; ?>), but it’s not that problematic. Come on:

The WP makes any kind of query by posts through the Wp_query. The use of the loop as seen in templates most basic is, behind the scenes, using the global WP_Query. In some more specific cases, the global instance does not meet, so you have to create your.

The secret lies in knowing which parameters you should feed the new builder WP_Query for it to bring what you want. You need to limit the category (let’s assume that it has the ID 3), and order by meta_values of a certain meta_key. That stays

$query = new WP_Query( array(
        'meta_key'      => 'post_views_count', //a sua meta key
        'orderby'       => 'meta_value_num',
        'order'         => 'DESC', //ou ASC, você que escolhe
        'cat'           => 3
    )
);

With that, the core of your loop stays:

while ($query->have_posts()) {
    $query->the_post();
    //só partir pro abraço
}

After using a bespoke query, it is always good to reset the global ones to avoid conflicts. This is done with

wp_reset_postdata();

I believe this is how you achieve what you seek.

EDIT

As mentioned in the comments, I had cited the incorrect reset method. I edited the answer so that it is coherent.

  • 1

    The correct after a new WP_Query is wp_reset_postdata()

  • @well noted brasofilo. Reply already edited

0

Hello, this caio solution is very good and really right now if you want a filter by Date, for example: I want to show the most viewed of the last 15 days.

I made the following code, if anyone can improve thank you.

<?php
            $query = $wpdb->get_results("SELECT p.ID, pm.meta_value, p.post_date, p.post_content, p.post_title FROM wp_postmeta pm, wp_posts p WHERE p.ID = pm.post_id AND pm.meta_key = 'views' AND p.post_type = 'post' AND pm.meta_value <> '0' AND p.post_date > DATE_SUB(CURDATE(), INTERVAL 15 DAY) ORDER BY CAST(pm.meta_value as DECIMAL) DESC LIMIT 5");
                //var_dump($query);
                    foreach ( $query as $news )
                        {
                            //echo $news->ID."<br>";
                        $args = array('posts_per_page' => 1, 'p'=>$news->ID); //exibindo o artigo do de um determinado ID.
                        // The Query
                        $my_query = query_posts($args);
                        global $post;
                        foreach ($my_query as $post) {

                         <!-- COLOQUE AQUI OS DADOS DO ARTIGO PARA REPETIR -->

<?php 
                        }

                        // Reset Query
                        wp_reset_query();

                   }; 

                        ?>

Hugs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.