我在相关产品“TAX_QUERY”‘TERMS’值方面遇到问题

时间:2021-07-29 作者:onurtrkylmaz

我有两种自定义页面类型。第一个是;“产品”;第二个是;附件;。在产品页面上,有“附件”类别;“输入”;罗列立柱。输入对值进行编码,如;06010603等;。这些代码也是产品代码。当我用metabox输入产品代码时,功能会在某些区域启动。如产品表、产品图片等。如何在术语部分打印此代码。

提前感谢你们的帮助。

工作代码;

<?php $article_list     = get_post_meta         ( get_the_ID(), \'products_article_list\',    true);?>

    <div class="product_container">
        <div class="product_row">
            <div class="article_section">
                <div class="section_first">
                    <?php echo do_shortcode($article_list); ?>
                </div>
            </div>
            <div class="related_section">
                <div class="section_second">
                    <h3 class="heading"><?php echo esc_html( get_the_title() );?>\'s Accessories</h3>
                    <?php $args = array(
                        \'post_type\'         =>  \'accessories\',
                        \'post_status\'       =>  \'publish\',
                        \'posts_per_page\'    =>  12,
                        \'tax_query\'         =>  array(
                                                array(
                                                    \'taxonomy\' => \'inputs\',
                                                    \'field\'    => \'slug\',
                                                    \'terms\'    => \'0601\',
                                                ),
                                            ),
                        "orderby"           =>  array(
                                                    \'featured_until\' => \'RAND\',
                                                    \'date\' => \'ASC\'
                                                ),  
                        \'post__not_in\' => array ($post->ID),
                    );
                    $related_items = new WP_Query( $args );
                        if ($related_items->have_posts()) :
                        while ( $related_items->have_posts() ) : $related_items->the_post();?>
                        <div class="related_post">
                            <div class="image"><a href="<?php the_permalink();?>"><?php the_post_thumbnail(array(300,200), array(\'class\'=>\'img-responsive\') );?></a></div>
                            <div class="text"><h4><a href="<?php the_permalink();?>" title="<?php the_title();?>"><?php the_title();?></a></h4><?php the_excerpt();?></div>
                        </div>
                    <?php endwhile; endif; wp_reset_postdata();?>
                </div>
            </div>
        </div>
    </div>
代码不工作

<?php $article_list     = get_post_meta         ( get_the_ID(), \'products_article_list\',    true);?>

    <div class="product_container">
        <div class="product_row">
            <div class="article_section">
                <div class="section_first">
                    <?php echo do_shortcode($article_list); ?>
                </div>
            </div>
            <div class="related_section">
                <div class="section_second">
                    <h3 class="heading"><?php echo esc_html( get_the_title() );?>\'s Accessories</h3>
                    <?php $args = array(
                        \'post_type\'         =>  \'accessories\',
                        \'post_status\'       =>  \'publish\',
                        \'posts_per_page\'    =>  12,
                        \'tax_query\'         =>  array(
                                                array(
                                                    \'taxonomy\' => \'inputs\',
                                                    \'field\'    => \'slug\',
                                                    \'terms\'    => $article_list,
                                                ),
                                            ),
                        "orderby"           =>  array(
                                                    \'featured_until\' => \'RAND\',
                                                    \'date\' => \'ASC\'
                                                ),  
                        \'post__not_in\' => array ($post->ID),
                    );
                    $related_items = new WP_Query( $args );
                        if ($related_items->have_posts()) :
                        while ( $related_items->have_posts() ) : $related_items->the_post();?>
                        <div class="related_post">
                            <div class="image"><a href="<?php the_permalink();?>"><?php the_post_thumbnail(array(300,200), array(\'class\'=>\'img-responsive\') );?></a></div>
                            <div class="text"><h4><a href="<?php the_permalink();?>" title="<?php the_title();?>"><?php the_title();?></a></h4><?php the_excerpt();?></div>
                        </div>
                    <?php endwhile; endif; wp_reset_postdata();?>
                </div>
            </div>
        </div>
    </div>
我想使用此查询结果,因为我在许多地方使用此查询

<?php $article_list     = get_post_meta         ( get_the_ID(), \'products_article_list\',    true);?>

1 个回复
最合适的回答,由SO网友:Tim 整理而成

如何从中返回“products\\u article\\u list”元$article_list?

<?php $article_list = get_post_meta( get_the_ID(), products_article_list\', true);?>
它是一个ID列表,例如“06010603”还是一个数组?

tax_query terms参数需要一个数组。看见https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters

因此,如果它存储为ID列表,那么您需要做的就是explode 这个列表应该有用。

   ...
   \'tax_query\' =>  array(
       array(
           \'taxonomy\' =>  \'inputs\',
           \'field\'    => \'slug\',
           \'terms\'    => explode(\',\', $article_list),
       ),
   ),
   ...