Title: Double count
Last modified: November 10, 2018

---

# Double count

 *  [hansstavleu](https://wordpress.org/support/users/hansstavleu/)
 * (@hansstavleu)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/double-count-2/)
 * Within a shortcode I like to count my page views. Every time when i count a view
   with a custom field, it doubles the count. It counts for 2 instead of 1.
    Does
   someone has any idea what could be the reason and how to solve it?
 *     ```
       function update_gelezen($postid) {
           $date = date('Y-m-d');
           update_field('leesdatum', $date, $postid);
           $now = get_field('leesteller',$postid);
           $now = (int) $now;
           $new = $now + 1;
           update_field('leesteller', $new, $postid);
       }
   
       add_shortcode('showportfolio', 'myportfolio');
       function myportfolio() {
         $result = ‘’;
         $query = new WP_Query(array(
               'post_type' => 'lscontentblock',
               'posts_per_page' => -1,
               'orderby'=>'date',
               'order'=>'ASC'
         ));
   
         while ($query->have_posts()) {
            $query->the_post();
            $post_id = get_the_ID();
            $result .= “[blahblahblah]”;
            update_gelezen($post_id);
         }
         wp_reset_query();
         return do_shortcode($result)  ;
       }
       ```
   
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fdouble-count-2%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/double-count-2/#post-10866914)
 * Your code is oddly creating shortcodes within a shortcode. It should just call
   the shortcode function instead to cut out the middleman parsing and such.
 * But your loop is using its own query variable, except where you get the post 
   ID. That is using the global query variable, which will return the post that 
   the original shortcode is on. The call to `wp_reset_query()` is not needed, because
   you didn’t mess up the global query (you used your own variable).
 *  Thread Starter [hansstavleu](https://wordpress.org/support/users/hansstavleu/)
 * (@hansstavleu)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/double-count-2/#post-10867877)
 * Thank you [@joyously](https://wordpress.org/support/users/joyously/)!
 * The reason the code uses a shortcode within is that it uses the shortcode of 
   a theme-layouyt-shortcode i do not know the function of.
    Perhaps because of 
   the fact that English isnt my native language, I do not understand your sentence`
   But your loop is using its own query variable, except where you get the post 
   ID. That is using the global query variable, which will return the post that 
   the original shortcode is on.`
 * Anyway, thank you very much for your effort to response! Highly appreciated.
 * Kind regards,
    Hans
 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/double-count-2/#post-10868855)
 * By using `$query = new WP_Query`, you are creating a query that is separate from
   the main query used in the page request. And then
 *     ```
       while ($query->have_posts()) {
          $query->the_post();
       ```
   
 * continues to use your local query variable. However, `$post_id = get_the_ID();`
   uses the global query variable, and not your local query variable. The global
   query was for whatever page that your `showportfolio` shortcode was on, not the
   post you are looking at in your loop.
 *  Thread Starter [hansstavleu](https://wordpress.org/support/users/hansstavleu/)
 * (@hansstavleu)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/double-count-2/#post-10869218)
 * Thank you very much, Joy!
    Food for thought so to say 🙂
 * Puzzled regards,
    Hans
    -  This reply was modified 7 years, 7 months ago by [hansstavleu](https://wordpress.org/support/users/hansstavleu/).

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Double count’ is closed to new replies.

## Tags

 * [custom field](https://wordpress.org/support/topic-tag/custom-field/)
 * [custom post type](https://wordpress.org/support/topic-tag/custom-post-type/)
 * [double count](https://wordpress.org/support/topic-tag/double-count/)
 * [query](https://wordpress.org/support/topic-tag/query/)
 * [value](https://wordpress.org/support/topic-tag/value/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 4 replies
 * 2 participants
 * Last reply from: [hansstavleu](https://wordpress.org/support/users/hansstavleu/)
 * Last activity: [7 years, 7 months ago](https://wordpress.org/support/topic/double-count-2/#post-10869218)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
