在一个查询参数中组合两个数组

时间:2022-03-03 作者:Ioannis Koukotzilas

我正在尝试使用排除追加销售post__not_in 但我已经有了价值,不知道如何将两者结合起来。

我试过这个:

\'post__not_in\' => get_upsell_ids(), [get_the_ID()]
但它不起作用。

我的代码:

function eligo_custom_related_products_by_label() {
    
    if (is_product() && has_term(\'records\', \'product_cat\')) {

        global $post;

        if ( ! $post ) {
            return;
        }

        $related_by_same_label = wp_get_post_terms($post->ID, \'pa_label\', [\'fields\' => \'slugs\']);

        $args = [
            \'post_type\' => \'product\',
            \'post_status\' => \'publish\',
            \'orderby\' => \'rand\',
            \'posts_per_page\' => 4,
            \'post__not_in\' => [get_the_ID()],
            \'tax_query\' => [
                \'relation\' => \'AND\',
                [
                    \'taxonomy\' => \'product_visibility\',
                    \'field\'    => \'name\',
                    \'terms\'    => array(\'outofstock\'),
                    \'operator\' => \'NOT IN\'
                ],
                [
                    \'taxonomy\' => \'pa_label\',
                    \'field\' => \'slug\',
                    \'terms\' => $related_by_same_label,
                    \'operator\' => \'IN\',
                ],
            ],
        ];

        $loop = new WP_Query($args);

        if ($loop->have_posts()) { ?>

            <section class="related products">
                <div class="related__inner">
                    <?php
                    $attribute_names = [\'pa_label\'];
                    foreach ($attribute_names as $attribute_name) {
                        $taxonomy = get_taxonomy($attribute_name);
                        if ($taxonomy && !is_wp_error($taxonomy)) {
                            $terms = wp_get_post_terms($post->ID, $attribute_name);
                            $terms_array = [];
                            if (!empty($terms)) {
                                foreach ($terms as $term) {
                                    $archive_link = get_term_link($term->slug, $attribute_name);
                                    $full_line = \'<a href="\' . $archive_link . \'">\' . $term->name . \'</a>\';
                                    array_push($terms_array, $full_line);
                                }
                                echo \'<h3 class="section__title">\' . esc_html__(\'More from\', \'eligo\') . \': \' . implode(\', \', $terms_array) . \'</h3>\';
                            }
                        }
                    }

                    woocommerce_product_loop_start(); 

                    while ($loop->have_posts()):
                        $loop->the_post();
                        wc_get_template_part(\'content\', \'product\');
                    endwhile;
                    
                    ?>
                </div>
            </section>
            <?php

        } 

        wp_reset_postdata();
    }
}
add_action(\'woocommerce_after_single_product_summary\', \'eligo_custom_related_products_by_label\', 20);
提前谢谢。

1 个回复
SO网友:Tom J Nowell

这不起作用,因为这不是合并两个数组的方式:

\'post__not_in\' => get_upsell_ids(), [get_the_ID()]
相反,您需要使用这两个数组并使用array_merge, 标准PHP函数:

https://www.php.net/manual/en/function.array-merge.php

注意__not_in 类型参数具有非常高的性能成本,并且不可扩展,此查询将非常慢,需要缓存。

相关推荐

ARRAY_MERGE内的Foreach循环

我需要在下拉菜单中获得用户显示名称的列表。我有查询权限,但我不确定如何准确地将我的用户foreach放入数组中。以下是我需要将用户注入的代码:$data[\'settings\'][\'advanced_options\'] = array_merge($data[\'settings\'][\'advanced_options\'], [ [ "label" => "Dynamic Option 1", //