WordPressで本文の自動分割

add_filter('content_pagination',function($pages, $post){
  if(!is_single()){return $pages;}
  $pages = explodePost($pages,$post);
  return $pages;
},10,2);

function explodePost($pages, $post){
  if(get_post_meta($post->ID,'mp_switch',true)){return $pages;}
  $content = $post->post_content;
  if(strpos($content,'//act.ballooon.jp/imp/imp')){ // for ballooon counter
    return $pages;
  }
  $len = mb_strlen($content,'utf-8');
  $insert = "<!--nextpage-->";
  $width  = 3000;
  if($len/$width<2){
    return $pages;
  }
  $content = preg_replace("/\r\n|\r|\n/", "\n", $content);
  $st = $width;
  foreach(range(1,(int)($len/$width)) as $i){
    if($st > $len){ break; }
    $content = preg_replace("/^.{0,$st}.+\?\n\n\K/us", $insert, $content);
    $st = mb_strrpos($content,$insert,0,'utf-8')+$width;
  }
  // pattern blockquate
  $pat = sprintf("/(<blockquote[^>]*>.*)%s(.*<\/blockquote>)/isu",preg_quote($insert,'/'));
  $content = preg_replace($pat,'${1}${2}',$content);

  $pat = sprintf("/%s(.{0,%d})$/isu",preg_quote($insert,'/'),$width/2);
  $content = preg_replace($pat,'${1}${2}',$content);

  $return = explode($insert,$content);
  $return = array_filter($return,'strlen');
  $return = array_values($return);
  if(!$return[0]){ return $pages; }
  return $return;
}

WordPressの関連記事をカテゴリーとタグからプラグインなしで取得する(自作)

global $wpdb;
$sql = 'SELECT object_id,COUNT(object_id) AS c FROM wp_term_relationships WHERE term_taxonomy_id IN (SELECT term_taxonomy_id FROM wp_term_relationships WHERE object_id = '.get_the_ID().') GROUP BY object_id ORDER BY c DESC, object_id DESC LIMIT 12;';
$object = $wpdb->get_results($sql);
$ids = [];
foreach($object as $v){
  if(get_the_ID() == $v->object_id){
    continue;
  }
  $ids[] = $v->object_id;
}
$params = [
'post_type' => 'post',
'include'=> $ids,
'orderby' => 'post__in',
];
$relposts = get_posts($params);
foreach ( array_slice($relposts,0,6) as $post ) :
  setup_postdata( $post );
endforeach;


SELECT object_id,COUNT(object_id) AS c FROM wp_term_relationships WHERE term_taxonomy_id IN (SELECT term_taxonomy_id FROM wp_term_relationships WHERE object_id = '.get_the_ID().') GROUP BY object_id ORDER BY c DESC, object_id DESC LIMIT 12;

このSQLが肝
このSQLでterm一覧を取得しその中の記事を集計して関連度の高い順に取得している
includeを使うとexcludeが効かないので取得時に除外
記事数も制御できないのでarray_sliceでカット
多めに取得しているのは非公開記事や固定ページを弾くため