前提・実現したいこと
ワードプレスの投稿を投稿ごとにカスタムフィールド(Advanced Custom Fields)の数値で並び替えをしたいです。
(初期値は新着順に表示がされます。)
WordPressのサイト上で簡単に並び替え&絞り込みする方法
こちらを参考に「価格順」のボタンを作成し、下記コードを書きました。
が、価格順のボタンを押しても、並び替えがされません。
URL自体は
https://example/post/
↓
https://example/post/?meta_key=price&orderby=meta_value_num&order=ASC
に変わるのですが、並び替え反映がされずに困っております。。。
詳しい方いらっしゃいましたら、ご教授いただけますと幸いです。
どうぞ、宜しくお願い致します。
該当のソースコード
php
1<div class="items"> 2 <div class="inner"> 3 <ul class="sort"> 4 //価格順に並び替えるボタン 5 <li><a href="<?php echo add_query_arg( array('meta_key' => 'price', 'orderby' => 'meta_value_num', 'order' => 'ASC'), get_pagenum_link(1) ); ?>">価格順</a></li> 6 </ul> 7 <div class="posts"> 8 <?php 9 /* (ステップ1)データの取得 */ 10 $paged = get_query_var('paged', 1); 11 $query = new WP_Query( 12 array( 13 'paged' => $paged, 14 'posts_per_page' => 15, 15 'post_type' => 'post', 16 'cat' => 6, 17 ) 18 ); 19 ?> 20 <?php 21 /* (ステップ2)データの表示 */ 22 if ( $query->have_posts() ) : ?> 23 24 <?php while ( $query->have_posts() ) : $query->the_post();?> 25 <article class="post"> 26 <a href="<?php the_permalink(); ?>"> 27 <figure class="figure"> 28 <img src="../img/XXX.png" alt=""> 29 </figure> 30 <div class="text"> 31 <h3 class="title"> 32 <?php //記事タイトルの先頭24文字を表示 33 if( mb_strlen( get_the_title( $next->ID ) ) > 24 ): 34 $title= mb_substr( get_the_title( $next->ID ), 0, 24 ) ; echo $title. '...' ; 35 else: 36 echo get_the_title( $next->ID ); 37 endif; 38 ?> 39 </h3> 40 <p class="price"> 41 <?php $myk_field_name = get_field('price');if($myk_field_name){ ?> 42 <?php echo number_format($myk_field_name); ?>円 43 <?php } ?> 44 </p> 45 </div> 46 </a> 47 </article> 48 <?php endwhile; ?> 49 </div> 50 //ページナビ 51 <?php wp_pagenavi(array('query' => $query)); ?> 52 <?php endif; wp_reset_postdata(); ?> 53 </div> 54</div>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/14 00:31
2020/01/14 01:10