• 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 to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • 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

    (@hansstavleu)

    Thank you @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

    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

    (@hansstavleu)

    Thank you very much, Joy!
    Food for thought so to say ??

    Puzzled regards,
    Hans

    • This reply was modified 6 years, 4 months ago by hansstavleu.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Double count’ is closed to new replies.