• Hellow there,

    First of all thanks for your plugin, my goal is to sort my posts by most viewed ones (DESC) on the Post widget of Elementor Pro. I have been struggling with my issue for quite a while and your plugin might be the solution if i get it to work.

    I’m using the plugin “Post Views Counter”, which allows me to display the number of views on each of my single posts pages by simply entering the shortcode [post-views]. I’ve made it into a custom post page template (Elementor Theme Builder). Can you please guide me trough the process of making the custom query to sort my posts by view count ?

    What i tried :

    1 – insert an Elementor Post Widget, set the display as i like it most.

    2 – Query section of the Posts Widget settings :
    – Choose source = Current query
    – Query ID = most_viewed_videos
    – Advances query option = custom field value (i hesitated with advanced order by options)
    – Custom Field Options = custom field contains value (i hesitated, because the plugin “Post View Counter” also displays the word “views : … ” with an icon before the word views, so it’s not only a value it’s also among words.).
    – Field name : ???? what should i write ?
    – Field value : ??? i can’t guess nor manually update the view values, what is asked here exactly ?

    Sorry if i use the wrong technical terms, or if what is ask is really easy, but i’m quite new to this. If you could guide me or even better, implement a function “sort posts by view count”, it would be truyly awesome.

    Thank you again for your work, i’m looking forward to read you. Many people would love this feature i think (there’s already a lot of asking on the forums). Your answer might help some others too

    • This topic was modified 4 years, 9 months ago by udniweca.
Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author Shabti Kaplan

    (@shabti)

    Hello there, thank you for reaching out!

    Do you know if Post View Counter adds a meta field to each post and if so, can you find the name of this field? If so, you can probably use our “dynamic order” option.

    Sincerely, Shabti

    Thread Starter udniweca

    (@udniweca)

    Hellow,

    Thank you so much for your answer. Well when i cheked for the custom meta fields in the post panel, i didn’t find anything, there is only a data in the settings bar on the right but it doesn’t show a meta field that i can clearly use. Here is a screenshot .

    So i went into the “query” file of the plugin “Post View Counter” , and it seems that the meta field would be ” post_views ” . But when i enter ” post_views ” in the Dynamic OrderBy Field of advanced query in the elementor post widget, it becomes empty. My only way to display something is to write ” views ” but then it only display one post.

    I’m kind of desperate here. I’ll paste you the query file of the plugin Post Views Counter in case it helps :

    <?php
    // exit if accessed directly
    if ( ! defined( ‘ABSPATH’ ) )
    exit;

    /**
    * Post_Views_Counter_Query class.
    *
    * @class Post_Views_Counter_Query
    */
    class Post_Views_Counter_Query {

    public function __construct() {
    // actions
    add_action( ‘pre_get_posts’, array( $this, ‘extend_pre_query’ ), 1 );

    // filters
    add_filter( ‘query_vars’, array( $this, ‘query_vars’ ) );
    add_filter( ‘posts_join’, array( $this, ‘posts_join’ ), 10, 2 );
    add_filter( ‘posts_groupby’, array( $this, ‘posts_groupby’ ), 10, 2 );
    add_filter( ‘posts_orderby’, array( $this, ‘posts_orderby’ ), 10, 2 );
    add_filter( ‘posts_fields’, array( $this, ‘posts_fields’ ), 10, 2 );
    add_filter( ‘the_posts’, array( $this, ‘the_posts’ ), 10, 2 );
    }

    /**
    * Register views_query var.
    *
    * @param array $query_vars
    * @return array
    */
    public function query_vars( $query_vars ) {
    $query_vars[] = ‘views_query’;

    return $query_vars;
    }

    /**
    * Extend query with post_views orderby parameter.
    *
    * @param object $query
    */
    public function extend_pre_query( $query ) {
    if ( isset( $query->query_vars[‘orderby’] ) && $query->query_vars[‘orderby’] === ‘post_views’ )
    $query->pvc_orderby = true;
    }

    /**
    * Modify the db query to use post_views parameter.
    *
    * @global object $wpdb
    * @param string $join
    * @param object $query
    * @return string
    */
    public function posts_join( $join, $query ) {
    $sql = ”;

    if ( ! empty( $query->query[‘views_query’] ) ) {
    if ( isset( $query->query[‘views_query’][‘year’] ) )
    $year = (int) $query->query[‘views_query’][‘year’];

    if ( isset( $query->query[‘views_query’][‘month’] ) )
    $month = (int) $query->query[‘views_query’][‘month’];

    if ( isset( $query->query[‘views_query’][‘week’] ) )
    $week = (int) $query->query[‘views_query’][‘week’];

    if ( isset( $query->query[‘views_query’][‘day’] ) )
    $day = (int) $query->query[‘views_query’][‘day’];

    // year
    if ( isset( $year ) ) {
    // year, week
    if ( isset( $week ) && $this->is_valid_date( ‘yw’, $year, 0, 0, $week ) ) {
    $sql = ” AND pvc.type = 1 AND pvc.period = ‘” . str_pad( $year, 4, 0, STR_PAD_LEFT ) . str_pad( $week, 2, 0, STR_PAD_LEFT ) . “‘”;
    // year, month
    } elseif ( isset( $month ) ) {
    // year, month, day
    if ( isset( $day ) && $this->is_valid_date( ‘ymd’, $year, $month, $day ) ) {
    $sql = ” AND pvc.type = 0 AND pvc.period = ‘” . str_pad( $year, 4, 0, STR_PAD_LEFT ) . str_pad( $month, 2, 0, STR_PAD_LEFT ) . str_pad( $day, 2, 0, STR_PAD_LEFT ) . “‘”;
    // year, month
    } elseif ( $this->is_valid_date( ‘ym’, $year, $month ) ) {
    $sql = ” AND pvc.type = 2 AND pvc.period = ‘” . str_pad( $year, 4, 0, STR_PAD_LEFT ) . str_pad( $month, 2, 0, STR_PAD_LEFT ) . “‘”;
    }
    // year
    } elseif ( $this->is_valid_date( ‘y’, $year ) ) {
    $sql = ” AND pvc.type = 3 AND pvc.period = ‘” . str_pad( $year, 4, 0, STR_PAD_LEFT ) . “‘”;
    }
    // month
    } elseif ( isset( $month ) ) {
    // month, day
    if ( isset( $day ) && $this->is_valid_date( ‘md’, 0, $month, $day ) ) {
    $sql = ” AND pvc.type = 0 AND RIGHT( pvc.period, 4 ) = ‘” . str_pad( $month, 2, 0, STR_PAD_LEFT ) . str_pad( $day, 2, 0, STR_PAD_LEFT ) . “‘”;
    // month
    } elseif ( $this->is_valid_date( ‘m’, 0, $month ) ) {
    $sql = ” AND pvc.type = 2 AND RIGHT( pvc.period, 2 ) = ‘” . str_pad( $month, 2, 0, STR_PAD_LEFT ) . “‘”;
    }
    // week
    } elseif ( isset( $week ) && $this->is_valid_date( ‘w’, 0, 0, 0, $week ) ) {
    $sql = ” AND pvc.type = 1 AND RIGHT( pvc.period, 2 ) = ‘” . str_pad( $week, 2, 0, STR_PAD_LEFT ) . “‘”;
    // day
    } elseif ( isset( $day ) && $this->is_valid_date( ‘d’, 0, 0, $day ) ) {
    $sql = ” AND pvc.type = 0 AND RIGHT( pvc.period, 2 ) = ‘” . str_pad( $day, 2, 0, STR_PAD_LEFT ) . “‘”;
    }

    if ( $sql !== ” )
    $query->pvc_query = true;
    }

    // is it sorted by post views?
    if ( ( $sql === ” && isset( $query->pvc_orderby ) && $query->pvc_orderby ) || apply_filters( ‘pvc_extend_post_object’, false, $query ) === true )
    $sql = ‘ AND pvc.type = 4’;

    // add date range
    if ( $sql !== ” ) {
    global $wpdb;

    $join .= ” LEFT JOIN ” . $wpdb->prefix . “post_views pvc ON pvc.id = ” . $wpdb->prefix . “posts.ID” . $sql;
    }

    return $join;
    }

    /**
    * Group posts using the post ID.
    *
    * @global object $wpdb
    * @param string $groupby
    * @param object $query
    * @return string
    */
    public function posts_groupby( $groupby, $query ) {
    // is it sorted by post views or views_query is used?
    if ( ( isset( $query->pvc_orderby ) && $query->pvc_orderby ) || ( isset( $query->pvc_query ) && $query->pvc_query ) || apply_filters( ‘pvc_extend_post_object’, false, $query ) === true ) {
    global $wpdb;

    $groupby = trim( $groupby );

    if ( strpos( $groupby, $wpdb->prefix . ‘posts.ID’ ) === false )
    $groupby = ( $groupby !== ” ? $groupby . ‘, ‘ : ”) . $wpdb->prefix . ‘posts.ID’;

    if ( ! isset( $query->query[‘views_query’][‘hide_empty’] ) || $query->query[‘views_query’][‘hide_empty’] === true )
    $groupby .= ‘ HAVING post_views > 0’;
    }

    return $groupby;
    }

    /**
    * Order posts by post views.
    *
    * @global object $wpdb
    * @param string $orderby
    * @param object $query
    * @return string
    */
    public function posts_orderby( $orderby, $query ) {
    // is it sorted by post views?
    if ( ( isset( $query->pvc_orderby ) && $query->pvc_orderby ) ) {
    global $wpdb;

    $order = $query->get( ‘order’ );
    $orderby = ( ! isset( $query->query[‘views_query’][‘hide_empty’] ) || $query->query[‘views_query’][‘hide_empty’] === true ? ‘post_views’ : ‘pvc.count’ ) . ‘ ‘ . $order . ‘, ‘ . $wpdb->prefix . ‘posts.ID ‘ . $order;
    }

    return $orderby;
    }

    /**
    * Return post views in queried post objects.
    *
    * @param string $fields
    * @param object $query
    * @return string
    */
    public function posts_fields( $fields, $query ) {
    if ( ( ! isset( $query->query[‘fields’] ) || $query->query[‘fields’] === ” ) && ( ( isset( $query->pvc_orderby ) && $query->pvc_orderby ) || ( isset( $query->pvc_query ) && $query->pvc_query ) || apply_filters( ‘pvc_extend_post_object’, false, $query ) === true ) )
    $fields = $fields . ‘, SUM( IFNULL( pvc.count, 0 ) ) AS post_views’;

    return $fields;
    }

    /**
    * Extend query object with total post views.
    *
    * @param array $posts
    * @param object $query
    * @return array
    */
    public function the_posts( $posts, $query ) {
    if ( ( isset( $query->pvc_orderby ) && $query->pvc_orderby ) || ( isset( $query->pvc_query ) && $query->pvc_query ) || apply_filters( ‘pvc_extend_post_object’, false, $query ) === true ) {
    $sum = 0;

    // any posts found?
    if ( ! empty( $posts ) ) {
    foreach ( $posts as $post ) {
    if ( ! empty( $post->post_views ) )
    $sum += (int) $post->post_views;
    }
    }

    // pass total views
    $query->total_views = $sum;
    }

    return $posts;
    }

    /**
    * Validate date helper function.
    *
    * @param string $type
    * @param int $year
    * @param int $month
    * @param int $day
    * @param int $week
    * @return bool
    */
    private function is_valid_date( $type, $year = 0, $month = 0, $day = 0, $week = 0 ) {
    switch ( $type ) {
    case ‘y’:
    $bool = ( $year >= 1 && $year <= 32767 );
    break;

    case ‘yw’:
    $bool = ( $year >= 1 && $year <= 32767 && $week >= 0 && $week <= 53 );
    break;

    case ‘ym’:
    $bool = ( $year >= 1 && $year <= 32767 && $month >= 1 && $month <= 12 );
    break;

    case ‘ymd’:
    $bool = checkdate( $month, $day, $year );
    break;

    case ‘m’:
    $bool = ( $month >= 1 && $month <= 12 );
    break;

    case ‘md’:
    $bool = ( $month >= 1 && $month <= 12 && $day >= 1 && $day <= 31 );
    break;

    case ‘w’:
    $bool = ( $week >= 0 && $week <= 53 );
    break;

    case ‘d’:
    $bool = ( $day >= 1 && $day <= 31 );
    break;
    }

    return $bool;
    }

    }

    • This reply was modified 4 years, 8 months ago by udniweca.
    Thread Starter udniweca

    (@udniweca)

    I’ll show you what it does on my interface :

    Elementor Post Widget => set to show 6 posts per page => dynamic order by field “views” => only one result. Screenshot here

    Elementor Post Widget => set to show 6 posts per page => dynamic order by field “post_views” => no results. Screenshot here

    • This reply was modified 4 years, 8 months ago by udniweca.
    • This reply was modified 4 years, 8 months ago by udniweca.
    Plugin Author Shabti Kaplan

    (@shabti)

    It looks like there is a conflict between the custom query id ‘popular’ and my plugin’s queries. I’ll see what I can do

    Plugin Author Shabti Kaplan

    (@shabti)

    Please update to 1.0.9 and try again

    Thread Starter udniweca

    (@udniweca)

    Hellow,

    I updated to 1.0.9 and the problem remains. If i enter “views” as the dynamic “order by field” i get only one post displayed, and if i write “post_views” instead i get nothing (while the elementor widget is set to display up to 6 posts’ thumbnails).

    I got it to work for a moment by entering “pvc_most_viewed_posts” as a query ID, but after re-installing my plugins i can’t get it to work anymore. So i’m back at square one. Thank you for the support, if you have any ideas on how to display posts by “views” i’m all ears ?? .

    Kind regards,

    Hi @udniweca ,

    The “Post Views Counter” does not put the stats values in the post_meta table. They are stored in wp_post_views instead. Unless you choose another view counter pluging, which stores the hit stat values in the meta table, I don’t think it is possible to use the Advanced OrderBy option to solve your problem.

    What I do is creating a cron job php script which periodically clones the hit stats values from wp_post_views to the wp_postmeta table, and use shabti’s suggestion “Advanced OrderBy”. It seems to be working well. But — remember to pad 0’s before the hit value as this plugin sort the custom value alphabetically. As “Post Views Counter” stores unsigned BIGINT(20), I would make it 00000000000000000012 stored in the postmen table if the view count is 12.

    Hi @shabti ,

    The other issue is that, when I use the Dynamic OrderBy function, there is nowhere I can define ascending or descending. (and I notice that it is always ordered descending). Is this setting defined anywhere?

    Thanks.

    Hi Guys, What i do to get the post views is to install WP-PostViews plugin.
    Add a field group with one field called Views using Advanced custom fields to posts.
    Set the the field to numeric and default value to 0.

    Then go ahead and use that field anyway you want to order your posts since you have a numeric value returned.

    REDACTED

    Hi Guys, What i do to get the post views is to install WP-PostViews plugin.
    Add a field group with one field called Views using Advanced custom fields to posts.
    Set the the field to numeric and default value to 0.

    Then go ahead and use that field anyway you want to order your posts since you have a numeric value returned.

    Can you be more precise? how do you increase the custom field with new visits?
    thank you

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Post by view count + elementor’ is closed to new replies.