Combine 2 arrays in a query parameter

I'm trying to exclude upsells using post__not_in but I already have a value and do not know how to combine the two.

I tried this:

'post__not_in' = get_upsell_ids(), [get_the_ID()]

But it is not working.

My code:

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);

Thanks in advance.

Topic array wp-query Wordpress

Category Web


This does not work because this is not how you merge two arrays:

'post__not_in' => get_upsell_ids(), [get_the_ID()]

Instead you need to take those 2 arrays and combine them using array_merge, a standard PHP function:

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

Note that __not_in type parameters have a very high performance cost and do not scale, this query will be very slow and require caching.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.