arie_cristianD

disable bottom related post if empty

Jul 29th, 2025
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.24 KB | None | 0 0
  1. add_filter( 'jnews_force_disable_related_post', 'disable_bottom_related_if_empty', 100 );
  2.  
  3. function disable_bottom_related_if_empty( $flag ) {
  4.     if ( ! $flag ) {
  5.         $post_per_page = get_theme_mod( 'jnews_single_number_post_related', 6 );
  6.         $match         = get_theme_mod( 'jnews_single_post_related_match', 'category' );
  7.         $tag           = $category = $result = array();
  8.  
  9.         $attr = array(
  10.             'number_post'  => $post_per_page,
  11.             'include_tag'  => implode( ',', $tag ),
  12.             'fields'       => 'ids', // untuk performa
  13.             'post__not_in' => array( get_the_ID() ),
  14.         );
  15.  
  16.         if ( $match === 'category' ) {
  17.             \JNews\Single\SinglePost::getInstance()->recursive_category( get_the_category(), $result );
  18.  
  19.             if ( $result ) {
  20.                 foreach ( $result as $cat ) {
  21.                     $category[] = $cat->term_id;
  22.                 }
  23.             }
  24.             if ( ! empty( $category ) ) {
  25.                 $attr['cat'] = implode( ',', $category );
  26.             }
  27.         } elseif ( $match === 'tag' ) {
  28.             $tags = get_the_tags();
  29.             if ( $tags ) {
  30.                 foreach ( $tags as $cat ) {
  31.                     $tag[] = $cat->term_id;
  32.                 }
  33.             }
  34.             if ( ! empty( $tag ) ) {
  35.                 $attr['tag__in'] = implode( ',', $tag );
  36.             }
  37.         }
  38.  
  39.         $query = new WP_Query( $attr );
  40.         if ( 0 === $query->found_posts ) {
  41.             return true;
  42.         }
  43.     }
  44.  
  45.         return $flag;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment