arie_cristianD

disable inline related post if empty

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