wordpress URLのクエリを取得して 記事一覧の内容をソート

カスタムフィールドに金額などを入力し、カテゴリーページでそれを昇順、降順でソートしたい。


試しにカテゴリーページに以下のような条件分岐とループを記述。
URLの「order」部分を取得して分岐させた。

<?php if (($_GET['order']) =='DESC') :?>

<?php
// DESC
query_posts('meta_key=price&orderby=meta_value_num&order=DESC&posts_per_page =20');
if(have_posts()) :
  while(have_posts()) :
    the_post(); ?>
    
<p><a href="<?php the_permalink();?>">a<?php the_title(); ?></a></p>

<?php  endwhile;
endif;?>

<?php else : ?>
// DESC以外
<?php
query_posts('meta_key=price&orderby=meta_value_num&order=ASC&posts_per_page =20');
if(have_posts()) :
  while(have_posts()) :
    the_post(); ?>
    
<p><a href="<?php the_permalink();?>"><?php the_title(); ?></a></p>

<?php  endwhile;
endif;?>

<?php endif; ?>


あとは以下のようなのURLをサイドメニュー等に入れておけば特定のカテゴリーで昇順、降順の記事一覧が作れる。

URLで

  • /category/?meta_key=price&orderby=meta_value_num&order=DESC
    • 降順
  • /category/?meta_key=price&orderby=meta_value_num&order=ASC
    • 昇順


Graham Central Station - Pow - YouTube