查询两次拉同一个帖子

时间:2015-08-06 09:50:44

标签: php wordpress

在寻求帮助和失败之后,我设法为Wordpress写了一个查询,它完全按照它的预期做了,但是它会把它假设的帖子拉到两次。我盯着并盯着标记,我不知道为什么要这样做。这是查询:

<?php 
$the_query = new WP_Query( array(
    'post_type' => 'product',
    'tax_query' => array(
        'taxonomy' => 'supplier-tax',
    ),
) );

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

<?php 
    $terms = get_the_terms( $post->ID, 'supplier-tax');
    foreach ( $terms as $term ) {
        $termID[] = $term->term_id;
    } 

    $my_query = new WP_Query( array(
        'post_type' => 'supplier',
        'tax_query' => array(
            'field' => 'slug',
            'terms' => '$termID',
        ),
    ) ); ?>

    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p class="supplier">Supplied by <strong><?php the_title(); ?></strong></p>
        <img src="<?php the_field('logo'); ?>">
    <?php endwhile; ?>
    <?php wp_reset_query(); ?>

<?php endwhile; ?>
<?php wp_reset_postdata(); ?>

如果有人可以提供帮助,或指出我出错的地方以便我可以从中学习,我真的很感激。

2 个答案:

答案 0 :(得分:0)

它将返回与分类法相关的所有帖子&amp;邮寄类型您的提供。

第一个回答:     

    $term_slug = get_query_var('term'); 

    $args = array(
        'post_type' => array('product','supplier'),
        'tax_query' => array(
                        'taxonomy' => 'supplier-tax',
                        'field' => 'slug',
                        'terms'    => $term_slug,
                    )
    );
    $my_query = new WP_Query( $args );
    ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p class="supplier">Supplied by <strong><?php the_title(); ?></strong></p>
            <img src="<?php the_field('logo'); ?>">
    <?php endwhile; ?>

&GT;

第二个答案:

$posts = get_adjacent_post( true, '', true, 'supplier-tax' ); echo '<pre>';print_r($posts);echo '</pre>';

答案 1 :(得分:0)

我解决了它:

以防万一有人在另一个term中使用相同Taxonomy Post Type的{​​{1}}查询帖子时遇到任何问题。我将在下面发布我的答案,因为我终于自己解决了这个问题。

<?php 

$terms = get_the_terms( $post->ID, 'supplier-tax');
foreach ( $terms as $term ) {
    $termID[] = $term->term_id;
}

$the_query = new WP_Query( array(
    'post_type' => 'supplier',
    'tax_query' => array(
        'taxonomy' => 'supplier-tax',
        'field' => 'slug',
        'terms' => '$termID',
    ),
) );

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

   <p class="supplier">Supplied by <strong><?php the_title(); ?></strong></p>
   <img src="<?php the_field('logo'); ?>">

<?php endwhile; ?>
<?php wp_reset_postdata(); ?>

我希望这有助于某人,因为这也很难找到解决方案。