web-dev-qa-db-ja.com

The LoopおよびWordPressのWP_Query()からスティッキーポストを除外する

次のスニペットは、「最近の投稿」をリストするサイドバーウィジェットからのものです。それがホームページにあり、私がそのページに目立つように私の最新のスティッキーポストを特集しているので、私はこのループでスティッキーをスキップしたいと思います。ただし、post_not_in=sticky_postsは効果がありません。

<?php
    $the_query = new WP_Query("showposts=$number&offset=1&order=ASC&post_not_in=sticky_posts");

    while ($the_query->have_posts()) : $the_query->the_post();
        $do_not_duplicate = $post->ID; ?>
1
Scott B

私は@ tnorthcuttの回答を query_posts()のスティッキパラメータについてのWordPressのコーデックス を使って作成した例えば、あなたはあなたのウェブサイトのルートにtest.phpとしてドロップし、あなたのドメインを置き換えて、このようなURLにナビゲートすることによってそれが実行されるのを見ることができます:

http://example.com/test.php

コードに関する注意事項 クエリ文字列と同等の配列 post__no_in引数をカンマで区切った文字列として渡すことができないため、WP_Query()に渡しました。

また、offset=1の代わりにoffset=0で始めることは、そうでなければクエリによって返される最初の投稿を除外することを意味します。もちろん、適用可能な投稿が1つ多いと仮定すると、$numberで指定された投稿の数を得ることができます。だからここにコードがあります:

<?php
header('Content-Type:text/plain');
include "wp-load.php";

$number = 5;

$the_query = new WP_Query(array(
  'showposts' => $number,
  'offset' => 1,  // This will cause the query to skip over first post
  'order' => 'ASC',
  'post__not_in' => get_option("sticky_posts"),
  ));
while ($the_query->have_posts()) : $the_query->the_post();
  the_title(); 
endwhile;
3
MikeSchinkel

クエリからすべてのスティッキーポストを除外したい場合は、

query_posts(array("post__not_in" =>get_option("sticky_posts")));

(from コーデックス から)

ただし、それは3.0以降でのみ動作します。 http://wordpress.org/support/topic/excluding-sticky-posts-using-query_posts

編集:下のあなたのコメントに応えて、これを試してみてください(私はこれがうまくいくかどうかわからないが、うまくいけばそれはあなたが始めましょう):

<?php 
$args=array(
    'showposts'=>'$number',
    'offset'=>'1',
    'order'=>'ASC',
    'post__not_in'=>get_option("sticky_posts")
    );
$the_query = new WP_Query($args);

    while ($the_query->have_posts()) : $the_query->the_post();

        $do_not_duplicate = $post->ID; ?>
1