キーワード検索のページネーションをcategoryページと同様に実装しようとしたところパーマリンクがうまく切り替わらず、
2ページ目に遷移するまではしっかりとできるのですが、1ページ目に戻る際に
http://home_url/2/1/
と言ったようになってしまいうまくページが切り替わりません、
search.php
php
1<?php 2 $search_word = get_search_query(); 3 $meta_query = array(); 4 $args = array(); 5 6 $meta_query[] = array( 7 'key' => 'カスタムフィールドスラッグ', 8 'value' => $search_word, 9 'compare' => 'LIKE' 10 ); 11 12 //if there is more than one meta query 'or' them 13 if(count($meta_query) > 1) { 14 $meta_query['relation'] = 'OR'; 15 } 16 17 // The Query 18 $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; 19 $args['paged'] = $paged; 20 $args['orderby'] = 'title'; 21 $args['posts_per_page'] = 10; 22 $args['order'] = 'ASC'; 23 $args['post_type'] = 'post'; 24 $args['_meta_or_title'] = $search_word; 25 $args['meta_query'] = $meta_query; 26 27 $the_query = new WP_Query($args); 28 29 $search_number = $the_query->found_posts; 30 while ($the_query -> have_posts()) { 31 $the_query -> the_post(); 32?> 33 //ここにループ文 34<?php 35 } $big = 999999999; // need an unlikely integer 36 echo paginate_links( array( 37 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), 38 'format' => '?paged=%#%', 39 'current' => max( 1, get_query_var('paged') ), 40 'total' => $the_query->max_num_pages 41 ) ); 42?>
functions.php
php
1add_action( 'pre_get_posts', function( $q ) 2{ 3 if( $title = $q->get( '_meta_or_title' ) ) 4 { 5 add_filter( 'get_meta_sql', function( $sql ) use ( $title ) 6 { 7 global $wpdb; 8 9 // Only run once: 10 static $nr = 0; 11 if( 0 != $nr++ ) return $sql; 12 13 // Modified WHERE 14 $sql['where'] = sprintf( 15 " AND ( %s OR %s ) ", 16 $wpdb->prepare( "{$wpdb->posts}.post_title like '%%%s%%'", $title), 17 mb_substr( $sql['where'], 5, mb_strlen( $sql['where'] ) ) 18 ); 19 20 return $sql; 21 }); 22 } 23});
このfunctionはカスタムフィールドからもキーワード検索ができるようにしたものです。
あなたの回答
tips
プレビュー