LoginSignup
0
0

More than 3 years have passed since last update.

WordPressで実行するクエリ(SQL)に手を加えたい場合の手法について

Posted at

やりたいこと

have_posts() ループの中で the_post() で投稿を取得する際のSQLで LIMIT を指定しないようにしたい

実現方法

have_posts() ループを実行する前に、query_posts() 関数を呼び出してクエリを改変する。

サンプルコード

<?php
    // 投稿数の制限を解除する
    $args = array(
        'post_type' => 'products',
        'fields' => 'ids',
        'posts_per_page' => -1
    );
    query_posts( $args );
    // 実行するSQLを確認しながらパラメータを調整する
    // global $wp_query;
    // var_dump( $wp_query );
    if (have_posts()) :
        while (have_posts()) : the_post();
            echo '<li>';
            the_title();
            echo '</li>';
        endwhile; 
    else:
        // 何も取得されなかった
    endif;
 ?>

参考URL

テンプレートタグ/query posts
posts_per_pageで全件表示
関数リファレンス/WP Query
wp-includes/query.php

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0