false, 'posts_per_page' => $updated_total_posts_count, 'no_found_rows' => true, 'post_status' => 'publish', 'post_type' => $current_post_type, 'orderby' => $related_posts_order_by, 'fields' => 'ids', 'order' => $related_posts_order, ); if ( 'tags' === $related_posts_based_on ) { $terms = get_the_tags( $post_id ); if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { $term_ids = wp_list_pluck( $terms, 'term_id' ); } $query_args['tag__in'] = $term_ids; } else { $terms = get_the_category( $post_id ); if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { $term_ids = wp_list_pluck( $terms, 'term_id' ); } $query_args['category__in'] = $term_ids; } $query_args = apply_filters( 'astra_related_posts_query_args', $query_args ); return new WP_Query( $query_args ); } /** * Render Featured Image HTML. * * @param int $current_post_id current post ID. * @param string $before Markup before thumbnail image. * @param string $after Markup after thumbnail image. * @param boolean $echo Output print or return. * @return string|null * * @since 3.4.0 */ function astra_get_related_post_featured_image( $current_post_id, $before = '', $after = '', $echo = true ) { $related_post_structure = astra_get_option_meta( 'related-posts-structure' ); if ( ! in_array( 'featured-image', $related_post_structure ) ) { return; } $post_thumb = apply_filters( 'astra_related_post_featured_image_markup', get_the_post_thumbnail( $current_post_id, apply_filters( 'astra_related_posts_thumbnail_default_size', 'large' ), apply_filters( 'astra_related_posts_thumbnail_itemprop', '' ) ) ); $appended_class = has_post_thumbnail( $current_post_id ) ? 'post-has-thumb' : 'ast-no-thumb'; $featured_img_markup = '
'; $featured_img_markup = apply_filters( 'astra_related_post_thumbnail', $featured_img_markup, $before, $after ); if ( false === $echo ) { return $before . $featured_img_markup . $after; } echo $before . $featured_img_markup . $after; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } /** * Render Post Title HTML. * * @param int $current_post_id current post ID. * * @since 3.4.0 */ function astra_get_related_post_title( $current_post_id ) { $related_post_structure = astra_get_option_meta( 'related-posts-structure' ); if ( ! in_array( 'title-meta', $related_post_structure ) ) { return; } $target = apply_filters( 'astra_related_post_title_opening_target', '_self' ); $title_tag = apply_filters( 'astra_related_post_title_tag', 'h3' ); do_action( 'astra_related_post_before_title', $current_post_id ); ?> < class="ast-related-post-title entry-title"> > ID; $related_posts_grid = astra_get_option( 'related-posts-grid', 2 ); $related_post_meta = astra_get_option( 'related-posts-meta-structure' ); $related_post_structure = astra_get_option_meta( 'related-posts-structure' ); $output_str = astra_get_post_meta( $related_post_meta ); $exclude_ids = apply_filters( 'astra_related_posts_exclude_post_ids', array( $post_id ), $post_id ); $related_posts_total_count = absint( astra_get_option( 'related-posts-total-count', 2 ) ); // Get related posts by WP_Query. $query_posts = astra_get_related_posts_by_query( $post_id ); if ( $query_posts ) { if ( ! $query_posts->have_posts() ) { return apply_filters( 'astra_related_posts_no_posts_avilable_message', '', $post_id ); } // Added flag to load wrapper section 'ast-single-related-posts-container' only once, because as we removed 'posts__not_in' param from WP_Query and we conditionally handle posts__not_in below so it needs to verify if there are other posts as well to load, then only we will display wrapper. $related_posts_section_loaded = false; do_action( 'astra_related_posts_loop_before' ); /** * WP_Query posts loop. * * Used $post_counter & ( $post_counter < $total_posts_count ) condition to manage posts in while loop because there is case where manual 'post__not_in' condition handling scenario fails within loop. * * # CASE EXAMPLE - If total posts set to 4 (where 'post__not_in' not used in WP_Query) so there is a chance that out of those 4 posts, 1 post will be currently active on frontend. * * So what will happen in this case - Within following loop the current post will exclude by if condition & only 3 posts will be shown up. * * To avoid such cases $post_counter & ( $post_counter < $total_posts_count ) condition used. * * @since 3.4.0 */ $post_counter = 1; $total_posts_count = $related_posts_total_count + 1; while ( $query_posts->have_posts() && $post_counter < $total_posts_count ) { $query_posts->the_post(); $post_id = get_the_ID(); if ( is_array( $exclude_ids ) && ! in_array( $post_id, $exclude_ids ) ) { if ( false === $related_posts_section_loaded ) { $grid_class = ( $related_posts_grid ) ? 'ast-grid-' . $related_posts_grid : 'ast-grid-2'; echo ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } do_action( 'astra_related_posts_loop_after' ); } } /** * Enable/Disable Single Post -> Related Posts section. * * @since 3.4.0 * @return void */ function astra_related_posts_markup() { if ( astra_target_rules_for_related_posts() ) { astra_get_related_posts(); } } add_action( 'astra_entry_after', 'astra_related_posts_markup', 10 ); /** * Adds custom classes to the array of body classes. * * @since 3.4.0 * @param array $classes Classes for the body element. * @return array */ function astra_related_posts_body_class( $classes ) { if ( astra_target_rules_for_related_posts() ) { $related_posts_grid = astra_get_option( 'related-posts-grid', 2 ); $classes[] = 'ast-related-posts-grid-' . esc_attr( $related_posts_grid ); } return $classes; } add_filter( 'body_class', 'astra_related_posts_body_class' );