WP_Comment_Query example


function render_block_core_comment_template( $attributes$content$block ) {
    // Bail out early if the post ID is not set for some reason.     if ( empty( $block->context['postId'] ) ) {
        return '';
    }

    if ( post_password_required( $block->context['postId'] ) ) {
        return;
    }

    $comment_query = new WP_Comment_Query(
        build_comment_query_vars_from_block( $block )
    );

    // Get an array of comments for the current post.     $comments = $comment_query->get_comments();
    if ( count( $comments ) === 0 ) {
        return '';
    }

    $comment_order = get_option( 'comment_order' );

    

        $prepared_args = apply_filters( 'rest_comment_query', $prepared_args$request );

        $query        = new WP_Comment_Query();
        $query_result = $query->query( $prepared_args );

        $comments = array();

        foreach ( $query_result as $comment ) {
            if ( ! $this->check_read_permission( $comment$request ) ) {
                continue;
            }

            $data       = $this->prepare_item_for_response( $comment$request );
            $comments[] = $this->prepare_response_for_collection( $data );
        }

function render_block_core_comments_pagination_next( $attributes$content$block ) {
    // Bail out early if the post ID is not set for some reason.     if ( empty( $block->context['postId'] ) ) {
        return '';
    }

    $comment_vars     = build_comment_query_vars_from_block( $block );
    $max_page         = ( new WP_Comment_Query( $comment_vars ) )->max_num_pages;
    $default_label    = __( 'Newer Comments' );
    $label            = isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ? $attributes['label'] : $default_label;
    $pagination_arrow = get_comments_pagination_arrow( $block, 'next' );

    $filter_link_attributes = static function() {
        return get_block_wrapper_attributes();
    };
    add_filter( 'next_comments_link_attributes', $filter_link_attributes );

    if ( $pagination_arrow ) {
        $label .= $pagination_arrow;
    }

function render_block_core_comments_pagination_numbers( $attributes$content$block ) {
    // Bail out early if the post ID is not set for some reason.     if ( empty( $block->context['postId'] ) ) {
        return '';
    }

    $comment_vars = build_comment_query_vars_from_block( $block );

    $total   = ( new WP_Comment_Query( $comment_vars ) )->max_num_pages;
    $current = ! empty( $comment_vars['paged'] ) ? $comment_vars['paged'] : null;

    // Render links.     $content = paginate_comments_links(
        array(
            'total'     => $total,
            'current'   => $current,
            'prev_next' => false,
            'echo'      => false,
        )
    );

    
if ( ! $post_id ) {
        return array();
    }

    $defaults    = array(
        'status'  => 1,
        'post_id' => $post_id,
        'order'   => 'ASC',
    );
    $parsed_args = wp_parse_args( $args$defaults );

    $query = new WP_Comment_Query();
    return $query->query( $parsed_args );
}

/** * Retrieves comment data given a comment ID or comment object. * * If an object is passed then the comment data will be cached and then returned * after being passed through a filter. If the comment is empty, then the global * comment variable will be used, if it is set. * * @since 2.0.0 * * @global WP_Comment $comment Global comment object. * * @param WP_Comment|string|int $comment Comment to retrieve. * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which * correspond to a WP_Comment object, an associative array, or a numeric array, * respectively. Default OBJECT. * @return WP_Comment|array|null Depends on $output value. */


        $comment_args['number'] = $per_page;
        $page                   = (int) get_query_var( 'cpage' );

        if ( $page ) {
            $comment_args['offset'] = ( $page - 1 ) * $per_page;
        } elseif ( 'oldest' === get_option( 'default_comments_page' ) ) {
            $comment_args['offset'] = 0;
        } else {
            // If fetching the first page of 'newest', we need a top-level comment count.             $top_level_query = new WP_Comment_Query();
            $top_level_args  = array(
                'count'   => true,
                'orderby' => false,
                'post_id' => $post->ID,
                'status'  => 'approve',
            );

            if ( $comment_args['hierarchical'] ) {
                $top_level_args['parent'] = 0;
            }

            
$per_page     = get_option( 'comments_per_page' );
        $default_page = get_option( 'default_comments_page' );
        if ( $per_page > 0 ) {
            $comment_args['number'] = $per_page;

            $page = (int) get_query_var( 'cpage' );
            if ( $page ) {
                $comment_args['paged'] = $page;
            } elseif ( 'oldest' === $default_page ) {
                $comment_args['paged'] = 1;
            } elseif ( 'newest' === $default_page ) {
                $max_num_pages = (int) ( new WP_Comment_Query( $comment_args ) )->max_num_pages;
                if ( 0 !== $max_num_pages ) {
                    $comment_args['paged'] = $max_num_pages;
                }
            }
            // Set the `cpage` query var to ensure the previous and next pagination links are correct             // when inheriting the Discussion Settings.             if ( 0 === $page && isset( $comment_args['paged'] ) && $comment_args['paged'] > 0 ) {
                set_query_var( 'cpage', $comment_args['paged'] );
            }
        }
    }

    
Home | Imprint | This part of the site doesn't use cookies.