WordPress 投稿者別、タクソノミー別、カテゴリー別等、色々な記事一覧の取得方法

この記事は年前に書かれました。不適当な記述を含む場合がありますので、参考程度に留めてください。

どうもこんばんは。今回のtipsは【Wordpress 投稿者別、タクソノミー別、カテゴリー別など、記事一覧の取得方法】に関してです。タイトルの都合上「など」で省きましたが以下の一覧取得方法をまとめます。自分が思い出せない時の為にメモとしてまとめておきます。

いろいろな記事一覧の取得

記事一覧の取得(通常)
記事一覧の取得(ランダム)
記事一覧の取得(古い順)
カテゴリー別記事一覧の取得
タクソノミー別記事一覧の取得
投稿者/著者を元にした記事一覧の取得
カスタムフィールド別記事一覧の取得
閲覧中記事と同カテゴリーの記事一覧

記事一覧の取得(通常)

いつものやつです。


<?php if(have_posts()):while(have_posts()):the_post(); ?>
<?php the_time("n.j"); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php endwhile; endif; ?>

記事一覧の取得(ランダム)

ランダムのやつです。


<?php query_posts(‘showposts=10&orderby=rand’);?>
<?php if(have_posts()):while(have_posts()):the_post();?>
<?php the_time("n.j"); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php endwhile;endif;?>

記事一覧の取得(古い順)

古い順です。新着順はorderby=DESCに変更してください。


<?php query_posts(‘showposts=10&orderby=ASC’);?>
<?php if(have_posts()):while(have_posts()):the_post();?>
<?php the_time("n.j"); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php endwhile;endif;?>

カテゴリー別記事一覧の取得

以下の例ではカテゴリーID「1」の記事一覧を取得します。


<?php
$posts = get_posts(‘numberposts=10&category=1’);
global $post;
?>
<?php if($posts): foreach($posts as $post): setup_postdata($post); ?>
<?php the_time("n.j"); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php endforeach; endif;?>

タクソノミー別記事一覧の取得

以下の例では投稿タイプ”sample”の記事一覧を取得します。


<?php if ( have_posts() ) : query_posts(‘post_type=sample&posts_per_page=10&paged=’.$paged);?>
<?php while (have_posts()) : the_post(); ?>
<?php the_time("n.j"); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php endwhile; endif; ?>

投稿者/著者を元にした記事一覧の取得

以下の例では投稿者ID「1」の記事一覧を取得します。


<?php if ( have_posts() ) : query_posts(‘posts_per_page=10&author=1&paged=’.$paged); ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_time("n.j"); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php endwhile; endif; ?>

カスタムフィールド別記事一覧の取得

カスタムフィールドのキー名「サンプル」の内容が「テスト」の記事一覧を取得します。


<?php
if ( have_posts() ) : query_posts(‘posts_per_page=10&meta_key=サンプル&meta_value=テスト&paged=’.$paged); ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_time("n.j"); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php endwhile; endif; ?>

閲覧中記事と同カテゴリーの記事一覧


<?php
$cat = get_the_category();
$catids = "";
foreach($cat as $catid){
$catids .= $catid->cat_ID.",";
}
$relposts = get_posts(‘orderby=rand&category=’.$catids.’&numberposts=10′);?>
<?php foreach($relposts as $post) : ?>
<?php the_time("n.j"); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php endforeach; ?>

以上になります。