• 解決済 naotone

    (@naotone)


    function.phpに下記のコードを記入したのですが、すべてのクエリに適応されてしましました。
    これを一部のクエリにのみ適応させる方法を教えてください。
    function.phpへのコードの追加ではなく、query_postsのみを使用した方法でも大丈夫です。

    以下 http://wpdocs.sourceforge.jp/テンプレートタグ/query_postsより抜粋

    30〜60日前の投稿を表示
    // Create a new filtering function that will add our where clause to the query
    function filter_where( $where = ” ) {
    // posts 30 to 60 days old
    $where .= ” AND post_date >= ‘” . date(‘Y-m-d’, strtotime(‘-60 days’)) . “‘” . ” AND post_date <= ‘” . date(‘Y-m-d’, strtotime(‘-30 days’)) . “‘”;
    return $where;
    }
    add_filter( ‘posts_where’, ‘filter_where’ );

    query_posts( $query_string );

2件の返信を表示中 - 1 - 2件目 (全2件中)
  • kz

    (@kz)

    「一部のクエリ」をどう区別するのか、って話ですけども、
    別途ファンクションを作ればとりあえず OK です。
    functions.php に以下を追加:

    function filter_where( $where = '' ) {
      $where .= " AND post_date >= '" . date('Y-m-d', strtotime('-60 days')) . "'" . " AND post_date <= '" . date('Y-m-d', strtotime('-30 days')) . "'";
      return $where;
    }
    function my_query_posts( $query ) {
      add_filter( 'posts_where', 'filter_where' );
      query_posts( $query );
      remove_filter( 'posts_where', 'filter_where' );
    }

    「一部のクエリ」は
    my_query_posts( 〜 );
    と書きます。

    トピック投稿者 naotone

    (@naotone)

    kzさま

    ありがとうございます。
    一部のクエリにのみ対応することができました。

    つい最近フォーラムに同じような質問が出ていましたね。
    (my_query_postsで検索すると出てきました)
    https://krittproxy.appspot.com/ja.forums.wordpress.org/topic/6081
    気づかずに質問してしまいすみませんでした。

2件の返信を表示中 - 1 - 2件目 (全2件中)
  • トピック「function.phpへ記入したコードを特定のquery_postsへ適応させる方法」には新たに返信することはできません。