Support

Account

Home Forums General Issues Views Counter Field Reply To: Views Counter Field

  • Hello,

    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.

    Loop page that contains the

    <?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(); ?> -->