dtxb75622 2015-07-31 11:54
浏览 30
已采纳

从另一个共享相同分类的帖子类型获取帖子?

I've got Two custom Post Types set up in Wordpress, One being called Products and the other Suppliers. Slugs for these are 'product' and one being 'supplier'.

These two post types share a custom taxonomy called Suppliers which slug is 'supplier-tax'.This then has lots of children which are the different suppliers.

Basically, What I am trying to do is when you are on a single post page for a product, I need to pull in a post for the supplier as well. I thought that the best way to do this with how I have set it up is to select the appropiate supplier from the supplier taxonomy when on the product page. And this then queries and gets post from the 'Supplier' Post ttype with the same selected Taxonomy.

I wrote this query which brings in posts from the taxonomy, however It needs me to tell it which taxonomy and which slug etc to bring in plus it doesnt query the different post type which makes it useless, however it was a start:

<?php 
$the_query = new WP_Query( array(
    'post_type' => 'product',
    'tax_query' => array(
        'taxonomy' => 'supplier-tax',
        'field' => 'slug',
        'terms' => 'supplier_1',

    ),
) );

while ( $the_query->have_posts() ) :
    $the_query->the_post(); ?>
        <?php the_title(); ?>
        <?php the_post_thumbnail('full'); ?>
        <?php the_content(); ?>

<?php endwhile; ?>


<?php wp_reset_postdata(); ?>

I've tried to adapt and include parts from queries I've fond on previous sources but I can't seem to crack it. This is my attempt at it:

<?php  
    $terms = wp_get_post_terms( $post->ID, 'supplier-tax' );
    if($terms){
        $supplier_terms = array();
        foreach ($terms as $term){
        $supplier_terms[] = $term->slug;
    }

    $original_query = $wp_query;
    $wp_query = null;
    $wp_query = new WP_Query( array(
        'post_type' => 'supplier',
        'tax_query' => array(
            array(
            'taxonomy' => 'supplier-tax',
            'field' => 'slug',
            'terms' => $supplier_terms, //the taxonomy terms I'd like to dynamically query
            'posts_per_page' => '-1'
            ),
        ),
        'orderby' => 'title',
        'order' => 'ASC'
    ) );

    if ( have_posts() ): ?>
        <?php while (have_posts() ) : the_post(); ?>
        <?php the_title(); ?>"><?php the_title(); ?>
    <?php endwhile; ?>
    <?php endif;
        $wp_query = null;
        $wp_query = $original_query;
        wp_reset_postdata(); 
    } 
?>

Has anyone got any ideas on what I'm doing wrong or how I can make this work?

  • 写回答

1条回答 默认 最新

  • dtdr84101 2015-08-03 08:24
    关注

    I managed to find a solution to my problem, whilst I don't think it is the cleanest markup, it works and does the job.

    <?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;
        }
        echo $termID[0]; 
    ?>
    
    
    <?php 
    $my_query2 = new WP_Query( array(
        'post_type' => 'supplier',
        'tax_query' => array(
            'field' => 'slug',
            'terms' => '$termID',
    
        ),
    ) ); ?>
    
        <?php while ($my_query2->have_posts()) : $my_query2->the_post(); ?>
            <?php the_title(); ?>
            <?php the_post_thumbnail('full'); ?>
            <?php the_content(); ?>
    
    <?php endwhile; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥15 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接