###前提・実現したいこと
WordPressで下記コードを使い、カテゴリとタグの重複回数を重視した関連記事を取得したいです。
各記事ページに1度アクセスがあると、関連記事のIDが配列でカスタムフィールドに追加されます。
コードは以下のページのものです。
タグとカテゴリで強弱を設定した関連記事を取得する。 | web.contempo.jp
###発生している問題
ローカルでテストし問題なく動作することを確認しましたが、サーバーにアップロードするとサイトが表示できない状態になります。
CPUとメモリの使用率も異常な値になっていましたので、数十人のアクティブユーザーに対し処理が追いつかないようです。
全ての記事に既に関連記事が登録されている状態からなら、新記事1つずつに処理が発生することは問題ないと思いますが、既存の記事が1,000以上あるため予め登録できないかと考えています。
具体的な方法でなくても構いませんので、教えて頂ければ幸いです。
どうぞよろしくお願いいたします。
###該当のソースコード
PHP
1//function.php 2<?php 3function get_related_posts( $show_post = 5, $target_posttype = '', $post_id = ''){ 4 if ( empty( $post_id ) ) $post_id = get_post()->ID; 5 if ( empty( $post_id ) ) return; 6 if ( empty( $target_posttype ) ) $target_posttype = get_post( $post_id )->post_type; 7 $ex_show_post = get_post_meta( $post_id, '_my_relatedposts_shownum', true ); // 関連記事の表示数 8 $rel_date = get_post_meta( $post_id, '_my_relatedposts_update', true ); // 関連記事の登録日 9 $modified_date = mysql2date( 'Ymd', get_lastpostmodified(), false ); // ブログの最終更新日 10 $posts_per_page = -1; // 検索するpost数 [-1] で全て 11 //以下、 関連記事が登録されていない場合 || 関連記事登録日よりもあとに記事が更新されている場合 || 表示関連記事数の設定が変わった場合、のみ新規で関連記事を設定する 12 if( empty($rel_date) || $modified_date > $rel_date || $show_post != $ex_show_post ){ 13 14 $rel_ids = array(); 15 $taxes = get_object_taxonomies( get_post( $post_id )->post_type, 'names' ); // 使用タクソノミを取得(カテゴリ+カスタムタクソノミ) 16 unset( $taxes['post_format'] ); // post_formatを除外 17 $tax_count = 0; 18 $tax_count = count($taxes); 19 $tax_array = array_fill( 0, $tax_count, '' ); 20 if ($taxes) { 21 foreach ( $taxes as $taxname ) { 22 $terms = get_the_terms( $post_id, $taxname ); 23 if ( $terms ){ 24 foreach ( $terms as $term ) { 25 $rel_ids[ $taxname ][] = $term->term_id; // タームIDを配列に入れる 26 } 27 $tax_array[] = array( 28 'taxonomy' => $taxname, // categoryとかpost_tagとか 29 'field' => 'id', // 'id' または 'slug' 30 'terms' => $rel_ids[ $taxname ], // int または string または array 31 'include_children' => false, // 子カテゴリを含まない 32 'operator' => 'IN', 33 ); 34 } 35 } 36 } 37 $args = array( 38 'post__not_in' => array( $post_id ), // このpostを除外 39 'posts_per_page' => $posts_per_page, // 検索するpost数 40 'post_status' => 'publish', // 公開postに限定 41 'post_type' => $target_posttype, // ターゲットポストタイプ 42 'tax_query' => array_merge( array('relation' => 'OR'), $tax_array ) 43 ); 44 $rel_query = get_posts( $args ); 45 if ( $rel_query ) : foreach ($rel_query as $rel ): 46 $rel_point = 0; 47 $set_id = $rel->ID; 48 foreach ( $taxes as $taxname ) { 49 $terms = get_the_terms( $set_id, $taxname ); 50 if ( is_array($terms)) : foreach ( $terms as $term ): 51 if ( isset( $rel_ids[ $taxname ]) && in_array( $term->term_id , (array)$rel_ids[ $taxname ] ) ){ // 関連IDを含むかチェック 52 $rel_point++; 53 if( $taxname == 'post_tag' ){ $rel_point++; } // ポストタグを他タクソノミより優先するようにポイントを補正 必要に応じてコメントアウトや変更をして下さい。 54 } 55 endforeach; endif; // end $terms foreach 56 } // end $taxes foreach 57 $rel_with[ $set_id ] = intval($rel_point); // 関連度数 58 endforeach; endif; 59 if ( !empty( $rel_with ) ) { 60 arsort( $rel_with ); // 関連度数でソート 61 $i = 0; 62 foreach ( $rel_with as $key => $val ) { 63 if( $i >= $show_post ) { 64 break; 65 } else { 66 $rel_posts[] = $key; 67 echo '<!-- POST ID:'.$key. ' / RELATION POINT:' .$val. ' -->' ."n"; 68 $i++; 69 } 70 } 71 update_post_meta( $post_id, '_my_relatedposts', $rel_posts ); 72 echo '<!-- RELATED POSTS FOR POST-'.$post_id. ' ARE UPLOADED!! -->' ."n"; 73 } else { 74 $no_related = false; 75 update_post_meta( $post_id, '_my_relatedposts', $no_related ); 76 } 77 $timestamp = date( 'Ymd' ); 78 update_post_meta( $post_id, '_my_relatedposts_update', $timestamp ); 79 update_post_meta( $post_id, '_my_relatedposts_shownum', $show_post ); 80 } 81 $return_array = get_post_meta( $post_id, '_my_relatedposts', true ); 82 return $return_array; 83} 84?> 85 86//single.php 87<?php 88$related_posts = get_related_posts(5); 89if ( $related_posts ): foreach( $related_posts as $rid ): ?> 90 91 <a href="<?php the_permalink($rid); ?>"><?php echo get_the_title($rid); ?></a> 92 93<?php endforeach; 94else: ?> 95 <p>関連する記事はありませんでした。</p> 96<?php endif; ?> 97
###試したこと
既存の全記事に対し予め関連記事を登録できないかと、ローカルで下記内容を書き足してみましたがうまく動作しませんでした。
$myposts = get_posts('posts_per_page=-1'); foreach($myposts as $mypost): $post_id = $mypost->ID;
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。