質問
メインクエリとサブクエリの違いをわかりやすく教えてください。
回答
メインクエリ:WordPressが自動で実行する「ページ表示用のメインの投稿取得」。
// メインクエリを表示
if (have_posts()) {
while (have_posts()) {
the_post();
the_title();
}
wp_reset_postdata();
}
サブクエリ:自分で追加で作る「別の投稿を取りたいときのクエリ」。
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
'meta_key' => 'views',
'orderby' => 'meta_value_num',
);
$subquery = new WP_Query($args);
// サブクエリをループして表示
if ($subquery->have_posts()) {
while ($subquery->have_posts()) {
$subquery->the_post();
the_title();
}
wp_reset_postdata();
}

コメントを残す