%s', $meta_title ); } } /** * Related post open box * * @since 1.0.0 * * @return void */ public function open_related_post() { $posts_columns = get_post_meta( get_the_ID(), 'related_posts_columns', true ); if ( empty( $posts_columns ) ) { $posts_columns = Helper::get_option( 'related_posts_columns' ); } echo sprintf( '
'; echo ''; } /** * Related post * * @since 1.0.0 * * @return array */ public function get_related_terms( $term, $post_id = null ) { $post_id = $post_id ? $post_id : get_the_ID(); $terms_array = array( 0 ); $terms = wp_get_post_terms( $post_id, $term ); foreach ( $terms as $term ) { $terms_array[] = $term->term_id; } return array_map( 'absint', $terms_array ); } /** * Related post * * @since 1.0.0 * * @return void */ public function related_post() { // Only support posts if ( 'post' != get_post_type() ) { return; } if ( get_post_meta( get_the_ID(), 'hide_related_posts', true ) == 1 ) { return; } $posts_numbers = get_post_meta( get_the_ID(), 'related_posts_numbers', true ); if ( empty( $posts_numbers ) ) { $posts_numbers = Helper::get_option( 'related_posts_numbers' ); } global $post; $args = array( 'post_type' => 'post', 'posts_per_page' => $posts_numbers, 'ignore_sticky_posts' => 1, 'no_found_rows' => 1, 'order' => 'rand', 'post__not_in' => array( $post->ID ), 'tax_query' => array( 'relation' => 'OR', array( 'taxonomy' => 'category', 'field' => 'term_id', 'terms' => $this->get_related_terms( 'category', $post->ID ), 'operator' => 'IN', ), array( 'taxonomy' => 'post_tag', 'field' => 'term_id', 'terms' => $this->get_related_terms( 'post_tag', $post->ID ), 'operator' => 'IN', ), ), ); get_template_part( 'template-parts/post/related-posts', null, $args ); } }