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.
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.
Share Improve this question edited Mar 4, 2022 at 2:04 Tom J Nowell♦ 60.8k7 gold badges77 silver badges147 bronze badges asked Mar 3, 2022 at 23:44 Ioannis KoukotzilasIoannis Koukotzilas 11 bronze badge 1- 1 I've adjusted the question to avoid it being closed as offtopic, 3rd party plugin support is not in scope here, but this problem appears to be basic PHP rather than WordPress, namely that you haven't discovered how to merge 2 arrays/lists together, or recognised that this is what you were trying to do – Tom J Nowell ♦ Commented Mar 4, 2022 at 2:05
1 Answer
Reset to default 1This 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.