Support

Account

Home Forums ACF PRO Upvote/Downvote System using ACF Pro

Solved

Upvote/Downvote System using ACF Pro

  • Hi guys, first of all i want to point out the fact that i am a newbie and i can handle php on a basic basis. i want to create an upvote downvote system, by far i created two
    Number fields one called upvote and one called downvote. i manages to displayed them into a relationship that i use to display some posts. Here is where i got stuck:
    how do i send this information to the database, when i click a button to send a +1 to the database and save the field. not sure, but i imagine this will involve jquery right?

    i will show you what i have done by far:

    ` <?php

    $posts = get_field(‘display_list’);
    $upvote = get_field(‘upvote’);
    $downvote = get_field(‘downvote’);
    $postid = get_the_ID();
    $count = 0;

    if( $posts ): ?>

      <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
      <?php setup_postdata($post); $count++; ?>

    • <div class=”elements_display”>
      <div class=”vote_sys”>
      <?php echo $count;?>
      <div class=”votes”>
      <div class=”upvote”>
      <button type=”button”> <i class=”fas fa-arrow-up”></i></button>
      <span><?php echo get_field( “upvote”, get_the_ID() ); ?></span>

      </div>
      <div class=”downvote”>
      <button type=”button”><i class=”fas fa-arrow-down”></i></button>
      <span><?php echo get_field( “downvote”, get_the_ID() ); ?></span>
      </div>
      </div>

      </div>
      <div class=”elements_content”>
      <!– –><?/*=function_exists(‘thumbs_rating_getlink’) ? thumbs_rating_getlink() : ”*/?>

      “><?php the_title(); ?>
      “>

      <?php the_excerpt(); ?>
      </div>
      </div>

    • <div class=”clear”></div>
      <?php endforeach; ?>

    <?php wp_reset_postdata(); // IMPORTANT – reset the $post object so the rest of the page works correctly ?>
    <?php endif; ?>
    </div>

    Can you please at least give some advice, what to do, how can i do this ? i magine i have to create some functions since there is a foreach.

    Cheers, Lucian.

  • I don’t think you need ACF for this I built something similar to show if a post was helpful or not. Basically you add post meta to a post, by adding or removing instances of 1.

    If someone likes the post he clicks the button and the counter will go up by one. If someone doesn’t like it the reverse will happen.

    This is stored in post meta so no need for ACF. And you can easily use this meta to sort the posts with.

    You need 1 form with 2 options.
    option + = update post meta with +1
    option – = update post meta with -1

  • Can you give me a code example ?
    to be Honest, i want to use Numeric Acf, so the values could be also displayed into the wp-admin, and i only want positive values and i want to have them to be separated.

  • What do you mean with ‘separated’ ?

  • Sorry, i explained wrong, i want them to be different no calculation involved, upvote should echo the number of upvote arrow clicks and downvote should echo the downvote click.

  • But i imagine now it will be much easier for me to develop it using add_post_meta();
    Would you please be nice and enlight me with some code? i am not so good with developing stuff, i am just a scriptkid, at least tell me what should i use, if i am not asking too much.

    Cheers.

  • I only use it to ‘upvote’ FAQ posts, not downvote, so only the upvote part is here but I think you get the idea and can create the downvote, based of this. Plus the output is written in twig instead of php but that’s not an issue I think.

    Code in template (code is as is)

    <div class="faq__helpful">
        <h4>{{ __( 'Was this helpful', 'textdomain' ) }} ?</h4>
        <div>
            <form class="faq_helpful" action="" method="post">
                <input type="hidden" name="post_id" value="{{ post.ID }}" />
                <input type="hidden" name="sd_faq__nonce" value="{{ function( 'wp_create_nonce', 'sd-faq-nonce' ) }}" />
                <button type="submit" name="helpful_yes" value="1" class="button item__delete-submit">
                    {{ __( 'YES', 'textdomain' ) }}
                </button>
                {% if helpful_yes %}
                    &nbsp;&nbsp;
                    <small>{{ _n( '%s person found this helpful', '%s people found this helpful', helpful_yes, 'textdomain')|format(helpful_yes) }}</small>
                {% endif %}
            </form>
        </div>
    </div>
    if ( isset( $_POST[ "sd_faq__nonce" ] ) ) {
        if ( wp_verify_nonce( $_POST[ "sd_faq__nonce" ], 'sd-faq-nonce' ) ) {
    
            // add helpful counter
            if ( isset( $_POST[ "helpful_yes" ] ) && '1' == $_POST[ "helpful_yes" ] ) {
                $faq_id = get_the_ID();
                if ( $hf_yes == false ) {
                    update_post_meta( $faq_id, 'sd_helpful_yes', '1' );
                } else {
                    $hf_yes = $hf_yes + 1;
                    update_post_meta( $faq_id, 'sd_helpful_yes', $hf_yes );
                }
            }
        }
    }

    Since this is not an ACF issue/topic I don’t wanna get too deep into this. WordPress.stackexchange.com is a better place for all WP questions.

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

The topic ‘Upvote/Downvote System using ACF Pro’ is closed to new replies.