Title: foreachで出力された記事をソート
Last modified: 2022年9月9日

---

# foreachで出力された記事をソート

 *  解決済 [fumidalat](https://ja.wordpress.org/support/users/fumidalat/)
 * (@fumidalat)
 * [4年、 2ヶ月前](https://ja.wordpress.org/support/topic/foreach%e3%81%a7%e5%87%ba%e5%8a%9b%e3%81%95%e3%82%8c%e3%81%9f%e8%a8%98%e4%ba%8b%e3%82%92%e3%82%bd%e3%83%bc%e3%83%88/)
 * こんにちは。
    category.phpで下記のように子カテゴリーに属する記事の一覧を出力し
   ています。 この記事をソートして並び替えたいのですが、うまくいきません。
 *     ```
       <?php
         $cates = get_the_category();
         $deepest = 0;
         foreach($cates as $cate) {
             // カテゴリの祖先オブジェクトのIDの配列を取得
             $ancestors_ids = get_ancestors( $cate->cat_ID, 'category' );
             // 祖先オブジェクトの個数を取得
             $ancestor_num = count($ancestors_ids);
             // 祖先オブジェクトの個数が一番多いカテゴリのIDを取得
             if( $ancestor_num > $deepest) {
                 $deepest = $ancestor_num;
                 $deepest_id = $cate->cat_ID;
             }
         }
         $args = array(
             'category__in' => $deepest_id,         // カテゴリーIDを配列で指定
         );
         $query = new WP_Query($args);
         if( $query -> have_posts() ): while ($query -> have_posts()) : $query -> the_post();
       ?>
        <section class="child-box w23  bigger">
       		<a href="<?php the_permalink(); ?>"><figure class="mb15">
       <?php $value = get_post_meta($post->ID, 'list-img', true);?>
       <?php if(empty($value)):?>
       <img src="<?php echo get_template_directory_uri(); ?>/images/no-img.png
       " alt=""><?php else:?>
       <img src="<?php the_field('list-img'); ?>"  /><?php endif;?>
       			</figure>
       			<h2><?php the_title(); ?></h2>
       		<p class="right mt10">
       <?php $value6 = get_post_meta($post->ID, 'price', true); ?>
       <?php if(empty($value6)): ?>
       <?php else: ?>
       			<?php
       $price = number_format(get_field('price'));
       echo $price . '円';
       ?>
       <?php endif; ?>
       </p></a>
       </section>
       <?php endwhile; endif; wp_reset_postdata(); ?>
       ```
   
 * 通常のループであれば
 *     ```
       <div class="select flex-ed">
       		<p>並び順：</p>
       		<form name="irekae" method="get" id="sort_form5" class="sort_form5">
              <select name='narabi' onChange="document.forms['irekae'].submit()" >
                 　<option value="sort1"<?php if(!isset($_REQUEST['narabi']) || $_REQUEST['narabi'] == 'sort1') echo ' selected="selected"';?>>新着順</option>
                   <option value="sort2"<?php if(isset($_REQUEST['narabi']) && $_REQUEST['narabi'] == 'sort6') echo ' selected="selected"';?>>価格順</option>
              </select>
       </form>
       <?php $sortset = $_GET['narabi']; ?>
       <?php $sortall = array(
         "sort1" => 'order=DESC&posts_per_page=6&paged=',
         "sort2" => 'order=ASC&orderby=meta_value_num&meta_key=price&posts_per_page=6&paged='
       ); ?>
   
       <?php if($sortset != '') {
              $sort = $sortall[$sortset];
           } else {
              $sort = $sortall["sort1"];} ?>
       <?php query_posts($sort . $paged); ?>
       ```
   
 *     ```
       <?php
       while ( have_posts() ) :
       the_post();
       ?>
       ```
   
 * 以下略
 * でソートできるのですが、foreachでのループの場合はどのようにすればソートが可能
   でしょうか
 * よろしくお願いします。
 * ヘルプの必要なページ: _[リンクを見るには[ログイン](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fja.wordpress.org%2Fsupport%2Ftopic%2Fforeach%25E3%2581%25A7%25E5%2587%25BA%25E5%258A%259B%25E3%2581%2595%25E3%2582%258C%25E3%2581%259F%25E8%25A8%2598%25E4%25BA%258B%25E3%2582%2592%25E3%2582%25BD%25E3%2583%25BC%25E3%2583%2588%2F%3Foutput_format%3Dmd&locale=ja)]_

1件の返信を表示中 - 1 - 1件目 (全1件中)

 *  トピック投稿者 [fumidalat](https://ja.wordpress.org/support/users/fumidalat/)
 * (@fumidalat)
 * [4年、 2ヶ月前](https://ja.wordpress.org/support/topic/foreach%e3%81%a7%e5%87%ba%e5%8a%9b%e3%81%95%e3%82%8c%e3%81%9f%e8%a8%98%e4%ba%8b%e3%82%92%e3%82%bd%e3%83%bc%e3%83%88/#post-11680266)
 * すみません。
    解決はしていないのですが、問題が別のところにあることがわかりました。
 * したがって本件は一度家帰結済みとします。

1件の返信を表示中 - 1 - 1件目 (全1件中)

 トピック「foreachで出力された記事をソート」には新たに返信することはできません。

 * フォーラム: [使い方全般](https://ja.wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1件の返信
 * 1人の参加者
 * 最後の返信者: [fumidalat](https://ja.wordpress.org/support/users/fumidalat/)
 * 最後の活動: [4年、 2ヶ月前](https://ja.wordpress.org/support/topic/foreach%e3%81%a7%e5%87%ba%e5%8a%9b%e3%81%95%e3%82%8c%e3%81%9f%e8%a8%98%e4%ba%8b%e3%82%92%e3%82%bd%e3%83%bc%e3%83%88/#post-11680266)
 * 状態: 解決済

## トピック

### 人気のトピック

### 返信がないトピック

### サポート関連ではないトピック

### 解決済みのトピック

### 未解決のトピック

### すべてのトピック
