Skip to:
Content
Pages
Categories
Search
Top
Bottom

Can’t get list of comments in activity feed


  • Koka boka
    Participant

    @koka777

    I’m trying to display comments from a activity feed post in a separate window. But I keep getting the message Error loading comments.

    Part of the code for reference.

    
    // AJAX handler for loading comments
    function bpcp_load_comments() {
        $activity_id = intval($_POST['activity_id']);
        $sort_order = sanitize_text_field($_POST['sort_order']);
    
        if (!$activity_id) {
            wp_send_json_error('Invalid activity ID');
        }
    
        // Receiving comments
        $comments = bp_activity_get_meta($activity_id, 'comments');
    
        if (!$comments) {
            wp_send_json_error('Error loading comments');
        }
    
        if ($sort_order === 'popular') {
            usort($comments, function($a, $b) {
                $a_votes = get_comment_meta($a['id'], 'votes', true);
                $b_votes = get_comment_meta($b['id'], 'votes', true);
                return $b_votes - $a_votes;
            });
        } else {
            usort($comments, function($a, $b) {
                return strtotime($b['date_recorded']) - strtotime($a['date_recorded']);
            });
        }
    
        wp_send_json_success($comments);
    }
    add_action('wp_ajax_bpcp_load_comments', 'bpcp_load_comments');
    add_action('wp_ajax_nopriv_bpcp_load_comments', 'bpcp_load_comments');
  • You must be logged in to reply to this topic.
Skip to toolbar