2016-03-18 7 views
0

私はElliot Condonの優れた高度なカスタムフィールドを使用して設定された3つの異なる変数に基づいて3つの特定のIDを取得するカスタムWordPressクエリを作成しようとしています。複数の可変ポストIDを持つWordpressクエリ

私は私の変数を設定するためにこれを使用しています:

<?php 
    $post1 = get_field('post_1'); 
    $post2 = get_field('post_2'); 
    $post3 = get_field('post_3'); 
?> 

をそして、私はこのようなカスタムクエリにこれらの変数を渡したい:

<?php 
    $post_list = array($post1, $post2, $post3); 
    foreach($post_list as $post_id) : 
     query_posts('p='.$post_id); 
     while (have_posts()) : the_post(); 
     // echo the post 
     endwhile; 
     wp_reset_query(); 
    endforeach; 
?> 

しかし、上記のようではありません。作業して壊れたページになります。誰でもどのように修正するためのアイデアがありますか?私は明らかに変数をクエリに間違って渡していますが、それを修正する方法を理解することはできません。

EDIT - SOLUTION

はここで働いて更新されたブロックです。 DACrosbyに大きな感謝!カスタムポストタイプのクエリを実行しているので、$argsでどのタイプを指定する必要がありました。

<div class="row"> 
    <?php 
    $post1 = get_field('related_1'); 
    $post2 = get_field('related_2'); 
    $post3 = get_field('related_3'); 
    $args = array(
     'post__in' => array($post1, $post2, $post3), 
     'post_type' => 'work' 
    ); 
    $the_query = new WP_Query($args); 
    ?> 
    <?php if ($the_query->have_posts()): ?> 
    <ul class="case-studies cf fade-in fade-in-3"> 
     <!-- Basic Projects --> 
     <?php while ($the_query->have_posts()) : $the_query->the_post(); ?> 
      <?php 
       //Get Featured Image URL 
       $feat_image = wp_get_attachment_url(get_post_thumbnail_id($post->ID)); 
       //Get Project Categories 
       $terms = get_the_terms($post->ID, 'type'); 
      ?> 
      <li class="case-study"> 
       <img src="<?php echo $feat_image; ?>" alt="<?php the_title(); ?>"> 
       <a href="<?php the_permalink(); ?>" class="cs-hover"> 
        <div class="col-table cs-text"> 
         <div class="col-tcell"> 
          <span><?php the_title(); ?></span> 
          <span class="divider"></span> 
          <span><?php the_field('description'); ?></span> 
          <?php if(get_field('services')): ?> 
           <ul class="tags"> 
            <?php while(has_sub_field('services')): ?> 
             <li><?php the_sub_field('service'); ?></li> 
            <?php endwhile; ?> 
           </ul> 
          <?php endif; ?> 
          <span class="text-link">View Project</span> 
         </div> 
        </div> 
       </a> 
      </li> 
     <?php endwhile; ?> 
    </ul> 
    <?php endif; ?> 
    <?php wp_reset_query(); ?> 
</div> 

答えて

1

From the documentation

警告 query_posts()は、データベースを照会し、ポストのリストを生成するために多くの中の唯一の方法です。 query_posts()を使用することを決定する前に、欠点を理解してください。

メインループの変更 query_posts()は、メインループを変更するためのものです。 ...

二次ループは二次リストを作成するには(例えば、ページ、またはサイドバーのウィジェット内のリンクのリストの一番下に関連する記事のリスト)は、WP_Queryかの新しいインスタンスを作ってみますget_posts()を使用してください。

An example of WP_Query usage:

// The Query 
$the_query = new WP_Query($args); 

// The Loop 
if ($the_query->have_posts()) { 
    echo '<ul>'; 
    while ($the_query->have_posts()) { 
     $the_query->the_post(); 
     echo '<li>' . get_the_title() . '</li>'; 
    } 
    echo '</ul>'; 
} else { 
    // no posts found 
} 
/* Restore original Post Data */ 
wp_reset_postdata(); 

そこから、あなただけのWP_Queryに送信された$ argsをを変更します。例:

$args = array(
     'post__in' => array($post1, $post2, $post3), 
     'post_type' => 'desired_post_type' 
); 
+0

私はこれを実装しようとしましたが、それでも動作させることはできません。私は上に私の完全なコードブロックを追加...多分私は間違っている場所にスポットすることができますか?私は本当にあなたの助けに感謝します! –

+0

すぐに間違っているとは思わない。何か間違いはありますか?あなたのサーバーのerror_logファイルをチェックするか、[WordPressでエラーを表示する](https://codex.wordpress.org/Debugging_in_WordPress) – DACrosby

+0

ログにエラーが表示されず、ページの残りの部分が正常に読み込まれています。しかし出力されているのは '

" "
'です。 –

関連する問題