• Resolved gperez-tl

    (@gperez-tl)


    Hi,
    I’m displaying a list of posts ordering by views. But the plugin is only taking in account the posts that have actual views. The ones never visited are not being displayed. Why?

    Reason I need this functionality is that i’m using the plugin to display items and reorder them by different criteria. All criteria shows all my items, but sorting items by “popularity” will retrieve much less results than the other filters and that a weird user experience.

    Any clue?

    Thanks in advance.

    https://wordpress.org/plugins/wp-postviews/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Lester Chan

    (@gamerz)

    It is a technical limitation. The meta_key will only be created for every post published after the plugin has been activated.

    There is nothing I can do here.

    Thread Starter gperez-tl

    (@gperez-tl)

    I understand. Thanks.

    Internally, I guess there’s an INNER JOIN involved? How can we see the generated query? Not sure if a LEFT JOIN would solve this at once. But I’m considering using a plain MySQL query to retrieve results (on my custom view).

    Thread Starter gperez-tl

    (@gperez-tl)

    Well, I solved it with something like this:

    function set_default_views_metakey($post_ID){
        add_post_meta($post_ID, 'views', 0, true);
        return $post_ID; // not sure if needed
    }
    
    add_action('wp_insert_post','set_default_views_metakey');

    It works. New posts now has zero as default views count.

    Thread Starter gperez-tl

    (@gperez-tl)

    And this will correct the error in existing posts:

    $args = array(
        //'post_type' => 'post',
        'meta_query' => array(
            'relation' => 'OR',
            array(
                'key' => 'views',
                'value' => 'bogus', // you have to pass a value
                'compare' => 'NOT EXISTS'
            ),
            array(
                'key' => 'views',
                'value' => ''
            )
        ),
        'fields' => 'ids'
      );
    
      // perform the query to get back an array of ids
      $posts = new WP_Query( $args );
    
      foreach ( $posts->posts as $id ) {
          add_post_meta($id, 'views', 0, true);
      }

    ref: http://wordpress.stackexchange.com/questions/121165/save-default-value-for-empty-or-missing-post-meta

    Plugin Author Lester Chan

    (@gamerz)

    @gperez-tl: thanks for sharing, this will work if you have a few post, but if your post tables is large, it will probably time out.

    But this is a good answer, to be honest I did not thought of this. I am going to pin this topic.

    Hi Lester,

    Have you updated the plugin with above issue?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Posts with no views are excluded. Why?’ is closed to new replies.