List POSTS in WordPress by views

1

I need to make sure that: <?php while ( have_posts() ) : the_post(); ?> lists according to the amount of views ( post_views_count ), it's like listing popular posts only that it will enter the% , and list according to which you have views .

People use mh_postmeta to list according to comments, but I need them to search for another table, but , this can only happen for a certain category. Let's say you have 40 CAT it should only list VIEW instead of DATA in CAT 3 .

>     
asked by anonymous 22.12.2015 / 21:00

2 answers

4

You have to do a slightly more complex query, which will lead you to look for a solution in addition to the main loop (i.e. <?php while ( have_posts() ) : the_post() ; ?> ), but that's not all that troublesome. Come on:

WP makes any type of posts query via WP_Query . The use of the loop as seen in the most basic templates is under the wipes, using the global WP_Query . In some more specific cases, the global instance does not answer, so you have to create yours.

The key is to know what parameters you should feed the new WP_Query constructor so that it brings what you want. You need to limit the category (let's assume it has ID 3), and sort by meta_value s  of a certain meta_key . This is

$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 this, the core of your loop becomes:

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

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

wp_reset_postdata();

I believe that you can achieve what you are looking for.

EDIT

As quoted in the comments, I had quoted the incorrect reset method. I have edited the answer so that it is consistent.

    
23.12.2015 / 13:45
0

Hello, this solution is very good and really right now if you want a filter by Data, for example: I want to display the most viewed in 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.

    
03.01.2017 / 12:15