From 7446d0fc4e80a2cacdf91cbb47795bc8e6829f01 Mon Sep 17 00:00:00 2001 From: Milana Date: Wed, 3 Nov 2021 12:44:02 +0100 Subject: [PATCH 1/5] Block class - render_callback() improvements. --- php/Block.php | 95 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 57 insertions(+), 38 deletions(-) diff --git a/php/Block.php b/php/Block.php index 0d1d2a6..595777e 100644 --- a/php/Block.php +++ b/php/Block.php @@ -63,37 +63,60 @@ public function register_block() { * @return string The markup of the block. */ public function render_callback( $attributes, $content, $block ) { - $post_types = get_post_types( [ 'public' => true ] ); - $class_name = $attributes['className']; - ob_start(); + $class_name = ''; + $post_types = get_post_types( array( 'public' => true ) ); + if ( isset( $attributes['className'] ) && ! empty( $attributes['className'] ) ) : + $class_name = $attributes['className']; + endif; + ob_start(); ?> -
-

Post Counts

+
> +

+
    $post_type_slug, - 'posts_per_page' => -1, - ] - ) - ); + $post_type_object = get_post_type_object( $post_type_slug ); + $post_count_object = wp_count_posts( $post_type_slug ); + if ( 'attachment' === $post_type_slug ) : + $post_count = $post_count_object->inherit; + else : + $post_count = $post_count_object->publish; + endif; ?> -
  • labels->name . '.'; ?>
  • - -

+
  • + labels->singular_name, + $post_type_object->labels->name + ); + ?> +
  • + + + + +

    + +

    ['post', 'page'], - 'post_status' => 'any', - 'date_query' => array( + $query = new WP_Query( array( + 'post_type' => array( 'post', 'page' ), + 'posts_per_page' => 5, + 'post_status' => 'any', + 'tag' => 'foo', + 'category_name' => 'baz', + 'post__not_in' => array( get_the_ID() ), + 'date_query' => array( array( 'hour' => 9, 'compare' => '>=', @@ -103,24 +126,20 @@ public function render_callback( $attributes, $content, $block ) { 'compare'=> '<=', ), ), - 'tag' => 'foo', - 'category_name' => 'baz', - 'post__not_in' => [ get_the_ID() ], - 'meta_value' => 'Accepted', - )); + ) ); - if ( $query->found_posts ) : + if ( $query->have_posts() ) : + ?> +

    +
      + have_posts() ) : $query->the_post(); + the_title( '
    • ', '
    • ' ); + endwhile; + wp_reset_postdata(); ?> -

      Any 5 posts with the tag of foo and the category of baz

      -
        - posts, 0, 5 ) as $post ) : - ?>
      • post_title ?>
      • -
      +
    + have_posts() ?>
    Date: Wed, 3 Nov 2021 16:54:57 +0100 Subject: [PATCH 2/5] Block class - render_callback() query optimisation. --- php/Block.php | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/php/Block.php b/php/Block.php index 595777e..e8ce17b 100644 --- a/php/Block.php +++ b/php/Block.php @@ -63,6 +63,7 @@ public function register_block() { * @return string The markup of the block. */ public function render_callback( $attributes, $content, $block ) { + $exclude = array( get_the_ID() ); $class_name = ''; $post_types = get_post_types( array( 'public' => true ) ); if ( isset( $attributes['className'] ) && ! empty( $attributes['className'] ) ) : @@ -110,13 +111,14 @@ public function render_callback( $attributes, $content, $block ) { array( 'post', 'page' ), - 'posts_per_page' => 5, - 'post_status' => 'any', - 'tag' => 'foo', - 'category_name' => 'baz', - 'post__not_in' => array( get_the_ID() ), - 'date_query' => array( + 'post_type' => array( 'post', 'page' ), + 'posts_per_page' => 10, + 'post_status' => 'any', + 'no_found_rows' => true, + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + 'date_query' => array( array( 'hour' => 9, 'compare' => '>=', @@ -133,8 +135,17 @@ public function render_callback( $attributes, $content, $block ) {