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; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端