0

I have got main product with meta key: addons and meta values in this key: 129456,968945,495435 Each of these three numbers is the key with these meta values. For example:

Post 1: meta_key: subproduct meta_value: 129456

Post 2: meta_key: subproduct meta_value: 968945

Post 3: meta_key: subproduct meta_value: 495435

And now I want to display these three posts in the main product. My code:

<?php if (!empty($addons = get_post_meta(get_the_ID(), 'addons', true))):?>
<?php
$params = array(
    'post_type' => 'product',
    'meta_key' => 'subproduct',
    'meta_value' => $addons
);
$wc_query = new WP_Query($params); 
?>

<?php while ($wc_query->have_posts()) : $wc_query->the_post(); ?>
<?php include(rh_locate_template('inc/parts/woomain.php')); ?>  
<?php endwhile; ?>


<?php wp_reset_postdata(); ?>   
<?php endif;?>

With one meta value it worked but with several it doesn't work anymore. How do you view these three posts?

Tom Harisond
  • 57
  • 1
  • 10

2 Answers2

0

Try to alter your query like this and lets see if that works

$params = array(
'post_type' => 'product',
'meta_query' => array(
    array(
        'key' => 'subproduct',
        'value' => array($addons),
        'compare' => 'IN'
    )
)
);
kaize
  • 793
  • 1
  • 9
  • 17
  • It also doesn't work. The problem here is that the query checks the entire meta value, and should each number separately. AND hm_postmeta.meta_value IN ('763820,761820'))) – Tom Harisond Apr 12 '20 at 19:23
0

It works with this code:

$params = array(
'post_type' => 'product',
'meta_query' => array(
    array(
        'key' => 'subproduct',
        'value' => $addons,
        'compare' => 'IN'
    )
)
);
Tom Harisond
  • 57
  • 1
  • 10