Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPBカップ
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

WordPressで複数ループによる重複投稿表示を回避する方法

WordPressの複数のループで表示される重複投稿を修正しようとしていますか?

複数のWordPressクエリーを使用して異なる投稿セットを表示している場合、重複コンテンツに遭遇する可能性があります。これは、いくつかの投稿が複数のループに一致し、二重に表示される可能性があるためです。

この投稿では、WordPressの複数ループによる重複投稿表示を簡単に回避する方法を紹介する。

Avoding duplicate posts when working with multiple WordPress loops

複数のWordPressループでDuplicator投稿が外観される方法

カスタムWordPressテーマやカスタムページテンプレートを作成する際、複数のWordPressループを使用する必要がある場合があります。

例えば、サイトの人気投稿の次に最近の投稿を表示したい場合があります。各カテゴリーの投稿をすべて表示することで、読者が興味深いコンテンツを見つけやすくなるかもしれません。

これらの例ではすべて、個別投稿が複数のループの条件に一致することがあります。このような場合、WordPressは重複したコンテンツを表示します。

このようなDuplicatorコンテンツは、あなたのサイトを乱雑でプロらしくないものにします。また、付加価値を与えることなく、画面上のスペースを取ってしまいます。

ループごとに投稿を動的に生成しているため、重複投稿が複数のループに表示されるかどうかを手動で予測することはできません。

ということで、WordPressで複数のループを扱う際に重複投稿を避ける簡単な方法を見てみよう。

複数のWordPressループにおける重複投稿の回避

このガイドでは、重複投稿エラーの原因となるWordPressコードのサンプルを紹介し、問題を解決するコードスニペットを共有します。

WordPressの子テーマやカスタムテンプレートを作成する場合、コードはまったく異なるものになるかもしれません。しかし、私たちのコードスニペットを出発点として使用し、あなた自身のサイトに合うように修正することができます。

まず、重複投稿の問題を作ってみましょう。以下のサンプルコードでは、「travel」カテゴリの投稿と「news」カテゴリの投稿を重複を避けてすべて表示しています:

/******  The First Query *******/
 
$first_query = new WP_Query(  array (
'category_name' => 'news',
'posts_per_page'  =>  3 
));
  
// The Loop
if ( $first_query->have_posts() ) {
    echo '<ul>';
    while ( $first_query->have_posts() ) {
        $first_query->the_post();
 
//display posts
echo '<li>';
echo the_post_thumbnail( array(50, 50) );
echo get_the_title(); 
echo '</li>';
    }
    echo '</ul>';
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
 
 
/******  The Second Query *******/
$second_query = new WP_Query(  array (
'category_name' => 'travel',
'posts_per_page'  =>  3
 
) );
 
// The Loop
if ( $second_query->have_posts() ) {
 
echo '<ul>';
while ( $second_query->have_posts() ) {
 
$second_query->the_post();
echo '<li>'; 
echo the_post_thumbnail( array(50, 50) );
echo get_the_title(); 
echo '</li>';
    }
echo '</ul>';
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>

ご覧のように、このコードはどちらのクエリーでも重複投稿をチェックしていません。

投稿が「ニュース」と「旅行」の両方のカテゴリーに属している場合、次の画像のように2回外観されます。

How to avoid duplicate post display with multiple loops in WordPress

この問題を解決しよう。

WordPressブログで投稿が重複しないようにするためには、最初のループで表示されたすべての投稿に関するデータを一時的に保存する必要がある。

この情報が得られたら、2つ目のクエリーを修正して、2つ目のループで重複投稿が表示されないようにすることができます:

/******  The First Query *******/
 
$first_query = new WP_Query(  array (
'category_name' => 'news',
'posts_per_page'  =>  3
) );
  
// The Loop
if ( $first_query->have_posts() ) {
    echo '<ul>';
    while ( $first_query->have_posts() ) {
        $first_query->the_post();
         
// Store Post IDs in an Array to reuse later
$exclude[] = $post->ID; 
 
//display posts
echo '<li>';
echo the_post_thumbnail( array(50, 50) );
echo get_the_title(); 
echo '</li>';
    }
    echo '</ul>';
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
 
 
/******  The Second Query *******/
$second_query = new WP_Query(  array (
'category_name' => 'travel',
'post__not_in'  =>  $exclude, // Tell WordPress to Exclude these posts
'posts_per_page'  =>  3
 
) );
 
// The Loop
if ( $second_query->have_posts() ) {
 
echo '<ul>';
while ( $second_query->have_posts() ) {
 
$second_query->the_post();
echo '<li>'; 
echo the_post_thumbnail( array(50, 50) );
echo get_the_title(); 
echo '</li>';
    }
echo '</ul>';
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>

上記のコードでは、投稿IDを$excludeという配列に格納しています。その後、2つ目のクエリーにpost__not_in引数を追加し、最初のループで表示された投稿を除外しています。

サイトにコードスニペットを追加することに慣れていない場合、WPCodeを使用するのが簡単です。WordPressのための最高のコードスニペットプラグインで、サイトを壊すリスクなしにカスタマイザーコードを追加することができます。

まず、WPCodeプラグインをインストールし、有効化する必要があります。WordPressプラグインのインストール方法については、こちらをご覧ください。

有効化した後、WordPressダッシュボードから「Code Snippets + Add Snippet」に進み、「Add Your Custom Code (New Snippet)」オプションをクリックします。

Add a new custom code snippet in WPCode

その後、カスタムコードをコードプレビューエリアに貼り付け、上部にタイトルを入力します。

また、ドロップダウンメニューをクリックして、「コードタイプ」をPHPスニペットとして選択する必要があります。

Enter custom code snippet

コードを入力したら、下にスクロールしてインサーターセクションを選択します。

ここでは、初期設定の「Auto Insert」を使用し、プラグインに自動的にコードをサイトに追加させることができます。

Insertion methods for snippets in WPCode

完了したら、スニペットを保存して有効化するだけです。

詳しくは、WordPressでカスタムコードを追加する方法をご覧ください。

これで、WordPressのサイトにアクセスすると、重複投稿が消えているのがわかるだろう。

Removing duplicate posts when using multiple posts in WordPress

この投稿が、WordPressで複数のループによる重複投稿表示を避ける方法を学ぶのにお役に立てば幸いです。また、最高のウェブデザインソフトウェアの選び方や、専門家が選ぶドラッグ&ドロップWordPressページビルダーもご覧ください。

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

情報開示 私たちのコンテンツは読者支援型です。これは、あなたが私たちのリンクの一部をクリックした場合、私たちはコミッションを得ることができることを意味します。 WPBeginnerの資金源 をご覧ください。3$編集プロセスをご覧ください。

アバター

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

究極のWordPressツールキット

ツールキットへの無料アクセス - すべてのプロフェッショナルが持つべきWordPress関連製品とリソースのコレクション!

Reader Interactions

16件のコメント返信を残す

  1. Syed Balkhi

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Nick J

    Is there a plugin that does this?

  3. Satriyo

    Could someone tell me how to do this? I’m really new to this and need a help, please give me a clear example with the post ID, how to store it? Let’s say, mine is 1527.

  4. Gaurav

    I’m running 2 loops before loops of a specific category in which I would like to avoid duplicates. So how do a store ID’s in the array from first two loops?

  5. Joe

    Just what I was looking for – thank you!

  6. Guilherme Alves

    Thank you soo much :))) This helps me alot!
    Save my day <3

  7. Julie

    AWESOME!! Thank you so much! And thank you SERGEYVLASOV for that last comment– Worked like a charm for my multiple loops. Hooray!! :)

  8. Pirooz

    This method just works fine until both of 2 loops located in one file.
    but when I put the first loop in the header.php and another one in the index.php,
    in_array($post->ID, $do_nit_duplicate) returns null.
    what can I do?

  9. warren

    Good afternoon all,

    will this work for my current issue with double display of posts on site? it literally displays a copy under the posts and the 1, 2, -> button…

    the site is I have deactivated re-activated plugins i am literally going nuts.

  10. agus

    can you help me?
    I have proble with duplicate category in my site

    please
    #thanks

  11. Gabriel

    Before iterating over the default loop shouldn’t we use wp_reset_postdata(); ?

  12. Greg

    @sergeyvlasov – Thanks that worked for me

    tho i changed

    $do_nit_duplicate to $do_not_duplicate

    ;)

  13. sergeyvlasov

    I think there is a flaw in this algorithm. It can spot no more than 1(one) duplication. So the magic line would look like
     
    $do_not_duplicate[] = $post->ID
     
    and then used as
     
    if(in_array($post->ID, $do_nit_duplicate)) continue;

返信を残す

コメントありがとうございます。すべてのコメントは私たちのコメントポリシーに従ってモデレートされ、あなたのメールアドレスが公開されることはありませんのでご留意ください。名前欄にキーワードを使用しないでください。個人的で有意義な会話をしましょう。