WordPress

get_postsを使用したサブループの作り方まとめ

ここでは、get_posts関数を使ったいろいろなサブループの作り方をまとめてみます。それぞれのコードの仕組みついては各記事をご参照ください。

コンテンツ

投稿でカテゴリを指定したサブループ

PHP

<?php
$cat_posts = get_posts(array(
    'post_type' => 'post', // 投稿タイプ
    'category' => 1, // カテゴリIDを番号で指定する場合
    'category_name' => 'スラッグ', // カテゴリをスラッグで指定する場合
    'posts_per_page' => 6, // 表示件数
    'orderby' => 'date', // 表示順の基準
    'order' => 'DESC' // 昇順・降順
));
global $post;
if($cat_posts): foreach($cat_posts as $post): setup_postdata($post); ?>
 
<!-- ループはじめ -->
<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
<p><?php the_category(', ') ?></p>
<p><?php the_time('Y/m/d') ?></p>
<p><?php the_excerpt(); ?></p>
<!-- ループおわり -->
 
<?php endforeach; endif; wp_reset_postdata(); ?>

投稿でタグを指定したサブループ

PHP

<?php
$tag_posts = get_posts(array(
    'post_type' => 'post', // 投稿タイプ
    'tag_id' => 1, // タグIDを番号で指定する場合
    'tag'    => 'スラッグ', // タグをスラッグで指定する場合
    'posts_per_page' => 6, // 表示件数
    'orderby' => 'date', // 表示順の基準
    'order' => 'DESC' // 昇順・降順
));
global $post;
if($tag_posts): foreach($tag_posts as $post): setup_postdata($post); ?>
  
<!-- ループはじめ -->
<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
<?php the_tags( '<p>', ', ', '</p>' ); ?>
<p><?php the_time('Y/m/d') ?></p>
<p><?php the_excerpt(); ?></p>
<!-- ループおわり -->
  
<?php endforeach; endif; wp_reset_postdata(); ?>

投稿でカテゴリとタグを組み合わせて指定したサブループ

PHP

<?php
$my_posts = get_posts(array(
    'post_type' => 'post', // 投稿タイプ
    'category' => 1, // カテゴリIDを番号で指定する場合
    'category_name' => 'スラッグ', // カテゴリをスラッグで指定する場合
    'tag_id' => 1, // タグIDを番号で指定する場合
    'tag'    => 'スラッグ', // タグをスラッグで指定する場合
    'posts_per_page' => 6, // 表示件数
    'orderby' => 'date', // 表示順の基準
    'order' => 'DESC' // 昇順・降順
));
global $post;
if($my_posts): foreach($my_posts as $post): setup_postdata($post); ?>
  
<!-- ループはじめ -->
<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
<p><?php the_time('Y/m/d') ?></p>
<p><?php the_excerpt(); ?></p>
<!-- ループおわり -->
  
<?php endforeach; endif; wp_reset_postdata(); ?>

表示中の投稿カテゴリ記事を一覧表示させるサブループ

PHP

<?php
global $post;
$post_id = $post->ID;
$cat = get_the_category($post_id);
$cat_id = $cat[0]->cat_ID;
  
$cat_posts = get_posts(array(
    'post_type' => 'post', // 投稿タイプ
    'category' => $cat_id, // カテゴリID
    'posts_per_page' => 6, // 表示件数
    'orderby' => 'date', // 表示順の基準
    'order' => 'DESC', // 昇順・降順
    'exclude' => $post_id // 表示中の投稿を除外
));
$count = count($cat_posts);
 
if($count>=1): foreach($cat_posts as $post): setup_postdata($post); ?>
 
<!-- ループはじめ -->
<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
<p><?php the_category(', ') ?></p>
<p><?php the_time('Y/m/d') ?></p>
<p><?php the_excerpt(); ?></p>
<!-- ループおわり -->
  
<?php endforeach; wp_reset_postdata(); else: ?>
   
<p>記事がありません。</p>
               
<?php endif ?>

表示中の投稿タグを記事一覧表示させるサブループ

PHP

<?php
global $post;
$post_id = $post->ID;
$tag = get_the_tags($post_id);
$tag_id = $tag[0]->term_id;
         
$tag_posts = get_posts(array(
    'post_type' => 'post', //投稿タイプ
    'tag_id' => $tag_id, // タグIDで指定する場合
    'posts_per_page' => 6, // 表示件数
    'orderby' => 'date', // 表示させる基準
    'order' => 'DESC', // 文字列の順番(昇順・降順)
    'exclude' => $post_id // 表示中の投稿を除外
));
$count = count($tag_posts);
             
if($count>=1): foreach($tag_posts as $post): setup_postdata($post);?>
   
<!-- ループはじめ -->
<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
<?php the_tags( '<p>', ', ', '</p>' ); ?>
<p><?php the_time('Y/m/d') ?></p>
<p><?php the_excerpt(); ?></p>
<!-- ループおわり -->
   
<?php endforeach; wp_reset_postdata(); else: ?>
    
<p>記事がありません。</p>
                
<?php endif ?>

カスタム投稿タイプで指定したタームのサブループ

PHP

<?php
$custom_posts = get_posts(array(
    'post_type' => 'カスタム投稿タイプスラッグ', // 投稿タイプ
    'posts_per_page' => 6, // 表示件数
    'orderby' => 'date', // 表示順の基準
    'order' => 'DESC', // 昇順・降順
    'tax_query' => array(
        array(
            'taxonomy' => 'タクソノミースラッグ', //タクソノミーを指定
            'field' => 'slug', //ターム名をスラッグで指定する
            'terms' => 'タームスラッグ', //表示したいタームをスラッグで指定
            'operator' => 'IN'
        ),
    )
));
global $post;
if($custom_posts): foreach($custom_posts as $post): setup_postdata($post); ?>
  
<!-- ループはじめ -->
<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
<p><?php the_time('Y/m/d') ?></p>
<p><?php the_excerpt(); ?></p>
<!-- ループおわり -->
  
<?php endforeach; wp_reset_postdata(); endif; ?>

カスタム投稿タイプで複数のタクソノミーからタームを指定したサブループ

PHP

<?php
$custom_posts = get_posts(array(
    'post_type' => 'カスタム投稿タイプスラッグ', // 投稿タイプ
    'posts_per_page' => 6, // 表示件数
    'orderby' => 'date', // 表示順の基準
    'order' => 'DESC', // 昇順・降順
    'tax_query' => array (
        'relation' => 'AND', //タクソノミー同士の関係を指定
        array (
            'taxonomy' => 'タクソノミースラッグ1',
            'field' => 'slug',
            'terms' => array ( 'スラッグ1', 'スラッグ2' ),
            'operator' => 'IN'
        ),
        array (
            'taxonomy' => 'タクソノミースラッグ2',
            'field' => 'slug',
            'terms' => array ( 'スラッグ3', 'スラッグ4' ),
            'operator' => 'IN'
        )
    )
));
global $post;
if($custom_posts): foreach($custom_posts as $post): setup_postdata($post); ?>
   
<!-- ループはじめ -->
<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
<p><?php the_time('Y/m/d') ?></p>
<p><?php the_excerpt(); ?></p>
<!-- ループおわり -->
   
<?php endforeach; wp_reset_postdata(); endif; ?>

表示中のカスタム投稿タイプのタームの記事一覧を表示させるサブループ

PHP

<?php
global $post;
$post_id = $post->ID;
$post_type = get_post_type();
$taxonomy = 'タクソノミースラッグ'; //タクソノミーを指定
$term = get_the_terms($post_id, $taxonomy);
$term_slug =  $term[0]->slug;
  
$custom_posts = get_posts(array(
    'post_type' => $post_type,
    'posts_per_page' => 6, // 表示件数
    'orderby' => 'date', // 表示順の基準
    'order' => 'DESC', // 昇順・降順
    'exclude' => $post_id, // 表示中の投稿を除外
    'tax_query' => array(
        array(
            'taxonomy' => $taxonomy,
            'field' => 'slug',
            'terms' => $term_slug,
            'operator' => 'IN'
        ),
    )
));
$count = count($custom_posts);
  
if($count>=1): foreach($custom_posts as $post): setup_postdata($post); ?>
  
<!-- ループはじめ -->
<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
<p><?php the_time('Y/m/d') ?></p>
<p><?php the_excerpt(); ?></p>
<!-- ループおわり -->
    
<?php endforeach; wp_reset_postdata(); else: ?>
    
<p>記事がありません。</p>
                
<?php endif ?>

表示中のカスタム投稿タイプで複数のタクソノミーからタームを絞り込み記事一覧させるサブループ

PHP

<?php
global $post;
$post_id = $post->ID;
$post_type = get_post_type();
 
$taxonomy01 = 'タクソノミースラッグ1'; //タクソノミー1を指定
$term01 = get_the_terms($post_id, $taxonomy01);
$term_slug01 =  $term01[0]->slug;
 
$taxonomy02 = 'タクソノミースラッグ2'; //タクソノミー2を指定
$term02 = get_the_terms($post_id, $taxonomy02);
$term_slug02 =  $term02[0]->slug;
 
$custom_posts = get_posts(array(
    'post_type' => $post_type,
    'posts_per_page' => 6, // 表示件数
    'orderby' => 'date', // 表示順の基準
    'order' => 'DESC', // 昇順・降順
    'exclude' => $post_id, // 表示中の投稿を除外
 
    'tax_query' => array (
        'relation' => 'AND', //タクソノミー同士の関係を指定
        array (
            'taxonomy' => $taxonomy01,
            'field' => 'slug',
            'terms' => $term_slug01,
            'operator' => 'IN'
        ),
        array (
            'taxonomy' => $taxonomy02,
            'field' => 'slug',
            'terms' => $term_slug02,
            'operator' => 'IN'
        )
    )
));
$count = count($custom_posts);
 
if($count>=1): foreach($custom_posts as $post): setup_postdata($post); ?>
 
<!-- ループはじめ -->
<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
<p><?php the_time('Y/m/d') ?></p>
<p><?php the_excerpt(); ?></p>
<!-- ループおわり -->
   
<?php endforeach; wp_reset_postdata(); else: ?>
 
<p>記事がありません。</p>
               
<?php endif ?>

表示中の投稿のカスタムフィールド値を1つ指定したサブループ

PHP

<?php
global $post;
$post_id = $post->ID;
$field_name = '項目名'; // カスタムフィールドの項目名
$value = post_custom( $field_name );
  
$my_posts = get_posts(array(
    'post_type' => 'post', // 投稿タイプ
    'meta_key' => $field_name, // カスタムフィールドの項目名
    'meta_value' => $value, // カスタムフィールドの値
    'posts_per_page' => 6, // 表示件数
    'orderby' => 'date', // 表示順の基準
    'order' => 'DESC', // 昇順・降順
    'exclude' => $post_id // 表示中の投稿を除外
));
$count = count($my_posts);
                        
if($count>=1): foreach($my_posts as $post): setup_postdata($post); ?>
  
<!-- ループはじめ -->
<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
<p><?php the_category(', ') ?></p>
<p><?php the_time('Y/m/d') ?></p>
<p><?php the_excerpt(); ?></p>
<!-- ループおわり -->
  
<?php endforeach; wp_reset_postdata(); else: ?>
  
<p>記事がありません。</p>
                 
<?php endif ?>

表示中の投稿のカスタムフィールド値を複数指定したサブループ

PHP

<?php
global $post;
$post_id = $post->ID;
 
$key01 = '項目名1'; // カスタムフィールドの項目名1
$key02 = '項目名2'; // カスタムフィールドの項目名2
$value01 = post_custom( $key01 ); // カスタムフィールドの値1
$value02 = post_custom( $key02 ); // カスタムフィールドの値2
  
$my_posts = get_posts(array(
    'post_type' => 'post', // 投稿タイプ
    'posts_per_page' => 6, // 表示件数
    'orderby' => 'date', // 表示順の基準
    'order' => 'DESC', // 昇順・降順
    'exclude' => $post_id, // 表示中の投稿を除外
    'meta_query' => array(
        array(
            'key' => $key01,
            'value'=> $value01
        ),
        array(
            'key' => $key02,
            'value'=> $value02
        )
    )
));
$count = count($my_posts);
                        
if($count>=1): foreach($my_posts as $post): setup_postdata($post); ?>
  
<!-- ループはじめ -->
<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
<p><?php the_category(', ') ?></p>
<p><?php the_time('Y/m/d') ?></p>
<p><?php the_excerpt(); ?></p>
<!-- ループおわり -->
  
<?php endforeach; wp_reset_postdata(); else: ?>
  
<p>記事がありません。</p>
                 
<?php endif ?>

カスタム投稿タイプをカスタムフィールドの値で並び替えさせるサブループ

PHP

<?php
$custom_posts = get_posts(array(
    'post_type' => 'カスタム投稿タイプスラッグ', // 投稿タイプ
    'posts_per_page' => 6, // 表示件数
    'meta_key' => 'カスタムフィールドのキー', //並び替えに利用したいカスタムフィールドのキーを指定
    'orderby' => 'meta_value', //カスタムフィールドの値で並び替え
    'order' => 'ASC' // 昇順・降順
    )
));
global $post;
if($custom_posts): foreach($custom_posts as $post): setup_postdata($post); ?>
    
<!-- ループはじめ -->
<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
<p><?php the_time('Y/m/d') ?></p>
<p><?php the_excerpt(); ?></p>
<!-- ループおわり -->
    
<?php endforeach; wp_reset_postdata(); endif; ?>

以上がget_postsを使用したサブループの作り方まとめでした。

最近の記事

  1. CSS

    レスポンシブウェブデザインの基本|メディアクリエリの設定
  2. CSS

    擬似クラス「:first-child」「:last-child」はどうして効かな…
  3. CSS

    要素の配置が簡単に決まるFlexboxの使い方
  4. WordPress

    WordPressの立ち上げからサイト構築までの初期手順
  5. CSS

    CSS|中央寄せが効かないときは・・・
PAGE TOP