###前提・実現したいこと
wordpressのキーワード検索結果とタグ検索結果を、
カスタムフィールドで作成した「ルビ」で、
五十音順ソートで表示したい。
###発生している問題・エラーメッセージ
・カスタムフィールドで作成した
「ルビ=ruby」「誤表記=false」を検索対象に設定する事には成功。
・「ルビ=ruby」で五十音順でソートが実現できません。(投稿順にソートがかかる)
※誤表記の五十音は無視したい
###該当のソースコード
■フィールド概要
フィールドラベル = ルビ
フィールド名 = ruby
フィールドタイプ = テキストエリア
function.php
php
1// カスタムフィールドを検索対象に含めます。 2function posts_search_custom_fields( $orig_search, $query ) { 3 if ( $query->is_search() && $query->is_main_query() && ! is_admin() ) { 4 // 4.4のWP_Query::parse_search()の処理を流用しています。(検索語の分割処理などはすでにquery_vars上にセット済のため省きます) 5 global $wpdb; 6 $q = $query->query_vars; 7 $n = ! empty( $q['exact'] ) ? '' : '%'; 8 $searchand = ''; 9 10 foreach ( $q['search_terms'] as $term ) { 11 $include = '-' !== substr( $term, 0, 1 ); 12 if ( $include ) { 13 $like_op = 'LIKE'; 14 $andor_op = 'OR'; 15 } else { 16 $like_op = 'NOT LIKE'; 17 $andor_op = 'AND'; 18 $term = substr( $term, 1 ); 19 } 20 $like = $n . $wpdb->esc_like( $term ) . $n; 21 // カスタムフィールド用の検索条件を追加します。 22 $search .= $wpdb->prepare( "{$searchand}(($wpdb->posts.post_title $like_op %s) $andor_op ($wpdb->posts.post_content $like_op %s) $andor_op (custom.meta_value $like_op %s))", $like, $like, $like ); 23 $searchand = ' AND '; 24 } 25 if ( ! empty( $search ) ) { 26 $search = " AND ({$search}) "; 27 if ( ! is_user_logged_in() ) 28 $search .= " AND ($wpdb->posts.post_password = '') "; 29 } 30 return $search; 31 } 32 else { 33 return $orig_search; 34 } 35} 36add_filter( 'posts_search', 'posts_search_custom_fields', 10, 2 ); 37// カスタムフィールド検索用のJOINを行います。 38function posts_join_custom_fields( $join, $query ) { 39 if ( $query->is_search() && $query->is_main_query() && ! is_admin() ) { 40 // group_concat()したmeta_valueをJOINすることでレコードの重複を除きつつ検索しやすくします。 41 global $wpdb; 42 $join .= " INNER JOIN ( "; 43 $join .= " SELECT post_id, group_concat( meta_value separator ' ') AS meta_value FROM $wpdb->postmeta "; 44 $join .= " WHERE meta_key IN ( 'false','ruby' ) "; 45 $join .= " GROUP BY post_id "; 46 $join .= " ) AS custom ON ($wpdb->posts.ID = custom.post_id) "; 47 } 48 return $join; 49} 50add_filter( 'posts_join', 'posts_join_custom_fields', 10, 2 ); 51 52// 検索結果ルビ五十音順に設定 53function customize_main_query($query) { 54 55 if ( is_admin() || ! $query->is_main_query() ) 56 return; 57 58 if ( $query->is_search() ) { 59 $query->set( 'posts_per_page', '100' ); 60 $query->set( 'order', 'ASC'); 61 $query->set( 62 'meta_query', 63 array( 64 'relation' => 'AND', 65 array( 66 'key' => 'ruby', 67 ) 68 ) 69 ); 70} 71 72 if ( $query->is_tag() ) { 73 $query->set( 'posts_per_page', '100' ); 74 $query->set( 'order', 'ASC'); 75 $query->set( 76 'meta_query', 77 array( 78 'relation' => 'AND', 79 array( 80 'key' => 'ruby', 81 ) 82 ) 83 ); 84} 85 86} 87add_action( 'pre_get_posts', 'customize_main_query' ); 88
search.php
php
1<?php get_header(); ?> 2 <div class="container"> 3<?php get_sidebar(); ?> 4 5<?php 6 global $wp_query; 7 $total_results = $wp_query->found_posts; 8 $search_query = get_search_query(); 9?> 10 11 <main class="main"> 12 <div class="contents"> 13 <section class="searchResults"> 14 <h1 class="searchResults__title">「 <?php echo $search_query; ?> 」の検索結果</h1> 15 <section class="searchResults__body"> 16 <ul class="searchResults__list"> 17 18<!-- 投稿情報 loop --> 19<?php 20if( $total_results >0 ): 21if(have_posts()): 22while(have_posts()): the_post(); 23?> 24 25 <li class="searchResults__listItem"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 26 27<!-- /post --> 28 <?php endwhile; endif; else: ?> 29 30<?php echo $search_query; ?> に一致する情報は見つかりませんでした。 31 32<?php endif; ?> 33 34 </ul> 35 </section> 36 </section> 37 </div> 38 </main> 39<?php get_sidebar(right); ?> 40 </div> 41<?php get_footer(); ?> 42
tag.php
php
1<?php get_header(); ?> 2 <div class="container"> 3<?php get_sidebar(); ?> 4 5<?php 6 global $wp_query; 7 $total_results = $wp_query->found_posts; 8 $search_query = get_search_query(); 9?> 10 11 <main class="main"> 12 <div class="contents"> 13 <section class="searchResults"> 14 <h1 class="searchResults__title">「 <?php single_tag_title(); ?> 」に関連する用語</h1> 15 <section class="searchResults__body"> 16 <ul class="searchResults__list"> 17 18<!-- 投稿情報 loop --> 19<?php 20if( $total_results >0 ): 21if(have_posts()): 22while(have_posts()): the_post(); 23?> 24 25 <li class="searchResults__listItem"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 26 27<!-- /post --> 28 <?php endwhile; endif; else: ?> 29 30<?php echo $search_query; ?> に一致する情報は見つかりませんでした。 31 32<?php endif; ?> 33 34 </ul> 35 </section> 36 </section> 37 </div> 38 </main> 39<?php get_sidebar(right); ?> 40 </div> 41<?php get_footer(); ?> 42
###試したこと
下記を参考に、カスタムフィールドを検索対象に設定しました。
http://wpcj.net/1363
下記を参考に、
ルビでの五十音順表示を試みましたが、実現できでおりません。
http://digipoke.com/archives/641
検索対象とする設定とルビでの五十音順表示の設定で、
打消しを起してる可能性を疑い、
試しに検索対象とする設定をコメントアウトした結果、文字通りただ検索対象から外れるだけでした。
お手引きよろしくお願いいたします。
###補足情報(言語/FW/ツール等のバージョンなど)
html5
css3
php5.6
mySOL5.6
wordpress4.5.2
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。