タイトル・本文以外のデータの取得
メインループの中では、タイトル・本文以外にも現在の投稿に付随する様々なデータが取得できます。確認していきましょう。
投稿ID
投稿IDは the_ID() 関数で出力できます。get_the_ID関数で出力せずに取得することも可能です。
投稿日時
投稿日時は the_date() 関数で出力できます。get_the_date関数で出力せずに取得することも可能です。
カテゴリの取得
カテゴリへのリンクは the_category() 関数で出力できます。
投稿者の取得
投稿者は the_author() 関数で出力できます。
メタ情報の取得
カスタムフィールドで設定したメタ情報(wp_post_metaテーブル内の情報)の取得は、the_postで投稿を取得した後に以下の関数を利用して行います。投稿を指定して、カスタムフィールドpriceを追加した後に、以下を実行して見ましょう
全てのメタ情報を取得する場合 get_post_custom()
<?php $custom_fields = get_post_custom(); ?>
<pre><?php var_dump($custom_fields); ?></pre>
項目を指定して取得する場合 get_post_meta()
現在の投稿について、カスタムフィールドpriceの情報を取得。
<?php $meta = get_post_meta(get_the_ID(), 'price'); ?>
<pre><?php var_dump($meta); ?></pre>
var_dumpの結果を確認すると、配列の形で取得されていることがわかります。
シンプルに文字列型で取得する場合には
<?php $meta = get_post_meta(get_the_ID(), 'price', true); ?>
<pre><?php var_dump($meta); ?></pre>
3つ目のオプションにtrueを渡すことで可能です。
コメントの取得
//シンプルな取得(現在の投稿)
<?php 
$comment_query = new WP_Comment_Query( array( 
    'post_id' => get_the_ID(),
) );
$comments = $comment_query->get_comments();
?>
//html内
<?php if ( $comments ) { ?>
    <ul>
    <?php foreach ( $comments as $comment ) { ?>
        <li><?php echo $comment->comment_content; ?></li>
    <?php } ?>
    </ul>
<?php } else { ?>
    <p>No comments found. </p>
<?php } ?>
WordPressループのような仕組みを持たないため、 get_commentsメソッドでコメントの配列を取得する必要があります。
queryメソッドでも同等の処理が可能です。
// 
<?php 
//あらかじめWP_Comment_Queryを生成
$comment_query = new WP_Comment_Query();
$comments = $comment_query->query(
    array( 
        'post_id' => get_the_ID(),
    )
);
上記のような形でWP_Comment_Queryを生成し、queryメソッドを呼び出すことでもコメントの配列が返ります。(内部的に上記のget_commentsメソッドが呼ばれます。)
さらに上記の処理を行う関数 get_commentsを用いて
$comments = get_comments(
    array( 
        'post_id' => get_the_ID(),
    )
);
とすることが可能です。
コメントの表示を行う場合、実際にはcomments.phpというテンプレートを通じて行うことがメインとなります。詳しくは後述します。
コメントのメタ情報の取得
get_comment_metaで取得
<?php $meta_values = get_comment_meta( $comment_id, $key, $single ); ?>
ユーザーの取得
WP_User_Queryで取得
<?php
$args = array(
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key'     => 'country',
            'value'   => 'Israel',
            'compare' => '='
        ),
        array(
            'key'     => 'age',
            'value'   => array( 20, 30 ),
            'type'    => 'numeric',
            'compare' => 'BETWEEN'
        )
    )
 );
// The Query
$user_query = new WP_User_Query( $args );
// User Loop
if ( ! empty( $user_query->results ) ) {
    foreach ( $user_query->results as $user ) {
        echo '<p>' . $user->display_name . '</p>';
    }
} else {
    echo 'No users found.';
}
?>
get_usersで同じことができる。
<?php
$blogusers = get_users( 'blog_id=1&orderby=nicename&role=subscriber' );
// WP_User オブジェクトの配列。
foreach ( $blogusers as $user ) {
    echo '<span>' . esc_html( $user->user_email ) . '</span>';
}
ログインユーザーはwp_get_current_userで取得できる。
//$userにユーザー情報が配列で格納されます
$user = wp_get_current_user();
ユーザーメタの取得
get_user_metaを利用する。
<?php 
  $user_id = 9;
  $key = 'last_name';
  $single = true;
  $user_last = get_user_meta( $user_id, $key, $single ); 
  echo '<p>The '. $key . ' value for user id ' . $user_id . ' is: ' . $user_last . '</p>'; 
?>
タームの取得
get_termsや get_taxonomiesで。 WPなんたらqueryは無い。
$categories = get_terms( 'category', array(
     'orderby'    => 'count',
     'hide_empty' => 0
 ) );
設定の取得
get_optionsで。
<h1><?php echo get_option( 'blogname' ); ?></h1>