おはようございます。@nyagihimeです。
仕事でちょっと使うことになりそうなので調べておいたのですがなかなかその案件が進まないので忘れないようにメモです。query_postsを利用してカスタムフィールドを利用した絞り込みつつ同様にカスタムフィールドの価格でソートする方法です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
query_posts( array( 'meta_query' => array( array( 'key' => 'itemdata1', 'value' => $_GET['param1'] ), array( 'key' => 'itemdata2', 'value' => $_GET['param2'] ), array( 'key' => 'itemdata3', 'value' => $_GET['param3'] ), array( 'key' => 'price', 'value' => $_GET['price'], 'type' => 'NUMERIC' ), ), 'relation' => 'AND', 'order' => $_GET['order'], 'orderby' => $_GET['orderby'] ) ); |
あとは、
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
param1<br> <input type="radio" value="データ1" name="param1" id="101"><label for="101">データ1</label> <input type="radio" value="データ2" name="param1" id="102"><label for="102">データ2</label> <input type="radio" value="データ3" name="param1" id="103"><label for="103">データ3</label> <br> param1<br> <input type="radio" value="データA" name="param1" id="101"><label for="101">データA</label> <input type="radio" value="データB" name="param1" id="102"><label for="102">データB</label> <input type="radio" value="データC" name="param1" id="103"><label for="103">データC</label> <br> param1<br> <input type="radio" value="データい" name="param1" id="101"><label for="101">データい</label> <input type="radio" value="データろ" name="param1" id="102"><label for="102">データろ</label> <input type="radio" value="データは" name="param1" id="103"><label for="103">データは</label> <br> order by<br> <input type="radio" value="date" name="orderby" id="date"><label for="date">DATE</label> <input type="radio" value="price" name="orderby" id="price"><label for="price">PRICE</label> <br> order<br> <input type="radio" value="asc" name="order" id="asc"><label for="asc">ASC</label> <input type="radio" value="desc" name="order" id="desc"><label for="desc">DESC</label> |
みたいなフォームを作って一覧ページに投げてあげればOK。GETでとるならパラメータをつなげたリンクを作って踏ませてもいいかもしれませんね。
ループが終わったら wp_reset_query();するのを忘れずに。
ポイントは、金額の時はデータを数値として扱わせるため、typeにNUMERICを指定してる所ですね。
こうすればカスタムフィールドの金額の欄に入ってるデータを数値として扱えるので金額によるソートができるわけですね。(何もしないと文字列として扱うらしい)
あとで時間のとれるときにもうちょっと詳しく書こうと思います。
コメントする