Support

Account

Home Forums General Issues Views Counter Field

Solving

Views Counter Field

  • Hello I need help for a views counter custom field. Please forgive the multiple posts. For some reason when I included it all in one, it would not post…

  • I’ve looked for hours for a straightforward tutorial where I can accomplish something which on its face seems like it would be rather simple. Unfortunately, all the posts and info I’ve found have not quite been what I’m looking for and I’m failing miserably. I’ve seen many about update_field, etc, but the info is too general for my working knowledge (eg: https://www.advancedcustomfields.com/resources/update_field/). I’m hoping someone will be able to supply what I need to accomplish this [now frustrating] task.

    Here is what I’m trying to do…

    I have a custom post type named ‘brochures’ and a “File” custom field (‘bro-spec-pdf’) which I’m using to echo the [‘url’] for a PDF download in a loop file (code below; listing_brochures-loop.php).

    I also have a second custom field (‘bro-spec-views’; currently a number field) I would like to use as a “Views” count so I know how many times that File[‘url’] field has been clicked for each post. I would like that count to increase 1 for each click.

    I understand that admin-ajax has to be used somehow, but all that I’ve tried has not worked. It’s also not my strong area, so it’s possible I just have no idea what I’m doing for that aspect.

    Can someone please lend a hand and provide what’s needed to get this done? Thank you in advance for any help you can provide.

  • Here is the Loop page that contains the code:

    <?php
    global $qode_options_elision;
    
    ?>
    <style>
    .icon_whitepaper.brospec:before {
        background-image: url(/wp-content/uploads/2019/10/brospec.png);
        height: 89px;
    }
    </style>
    <?php
    $pdf_doc = get_field( 'bro-spec-pdf', $post->ID );
    $pdf_url = $pdf_doc[ 'url' ];
    $pdf_name = $pdf_doc[ 'filename' ];
    ?>
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
      <div class="post_content_holder icon_whitepaper brospec">
        <div class="post_text">
          <h2><a href="<?php echo $pdf_url; ?>" title="<?php the_title_attribute(); ?>" id="brospec-link" class="brospec-link" target="_blank">
            <?php the_title(); ?>
            </a></h2>
          <?php the_excerpt(); ?>
          <div class="posted-readmore"> 
              <span style="font-weight:bold;"><?php
              $terms = get_the_terms( $post->ID, 'brochure_category' );
              foreach ( $terms as $term ) {
                echo $term->name;
              }
              ?></span>,
              Posted <span class="date">
            <?php the_time('F j, Y'); ?>
            </span> | <a href="<?php echo $pdf_url; ?>" id="brospec-link" title="<?php the_title_attribute(); ?>" class="brospec-link" target="_blank">
            <?php _e('DOWNLOAD','qode'); ?>
            </a>
        </div>
      </div>
    </article>
    <!-- #download-<?php the_ID(); ?> -->
  • Hello Friend, how are you?

    For you to count views or downloads of the pdf file, you need to change the workflow you have developed.

    Today, you provide the user with the direct link to the file (pdf or otherwise), so you can’t know who clicked or not.

    My suggestion to solve your problem, and first you create a template or a wp_ajax that receives by parameter the file that needs to be downloaded (or the post_id linked to the file).

    This adds +1 to the preview field and then redirects the user to the final file link.

  • Thank you for your response. I’m new to this this. How would something like what you suggested look?

  • Hi friend,

    I found an example of this method.

    https://www.kavoir.com/2010/05/simplest-php-hit-counter-or-download-counter-count-the-number-of-times-of-access-visits-or-downloads.html

    In the example it uses a txt file to store the counter number, but you can use an update_field ($ selector, $ value, [$ post_id]);

    greetings

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.