Linked Questions

Popular Questions

Comments like button only works for the first comment in wordpress code

Asked by At

I am currently working on a simple forum and then setting up a comment like button in wordpress where users can click on yes if the comment is helpful and displays the count of likes clicked on the front-end where the where the forum topics are listed. which actually worked but only worked for the first comment

I have coded the ajax call to retrieve the id and of the post for me to compare this is my first php/wordpress project. Thanks

This is located in my function.php file

add_action( 'rest_api_init', 'my_school_route' );

function my_school_route(){
    register_rest_route( 'myschool/v1', 'likebutton', array(
        'methods'   => 'POST',
        'callback'  => 'my_school_like_count'
    ) );

function my_school_like_count($data){

  $post_id_num =  $data['postId'];

        $likeID = wp_insert_post( array(
            'post_title'   => 'liker',
            'post_status'  =>'publish',
            'post_type'    => 'school_yes',
            'meta_input'   => array(
                'liked_comments'    => $post_id_num

        echo $likeID;


...and this is my javascript code

class Like {
        constructor() {


        events() {

            $('#helpful-botton').on('click', this.ourLikeButton.bind(this));



            var currentLikeBox = $("#helpful-botton");

            url: schoolManagerData.root_url + '/wp-json/myschool/v1/likebutton',
            type: 'POST', 
            success: (response) =>{
            error: (response) =>{



    var like = new Like();

at singular.php where the forum posts are listed

                $likeCount = new WP_Query(array(

                    'post_type'         => 'school_yes',
                    'meta_query'        => array(
                            'key'           => 'liked_comments',
                            'compare'       => '=',
                            'value'         => get_the_ID()
                ) );


            <div class="fr-col">
                <p class="fr-number"><?php echo $likeCount->found_posts ?></p>
                <p class="fr-txt">Helpful</p>

this is in comments.php where the button is located

                <div id="helpful-botton" action="#" class="helpful-btnbx">

                     <button id="helpful-botton" data-id="<?php the_ID(); ?>" class="helpful-btn">Yes</button>


i actually hoped it will work for all the comments but only worked for the first comment alone. Thanks for bearing with me.

Related Questions