• 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?

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    It depends on how meta values are actually stored. A string of comma separated values, an array, or multiple values pushed into the same key?

    By whatever means, parse out the individual values and construct a “meta_query” argument array that gets posts with any one of those meta values. Again depending on how the values are stored, you may need OR relation with LIKE operator. Somewhat like the second to last example in the Custom Field Parameters section of the docs page. Find the section in contents after the jump. Direct links to the section from here don’t work right.

    If you are managing post relations like I think you are, this is not a very efficient way to do that. You’d be better off using a custom taxonomy similar to tags to relate various posts.

Viewing 1 replies (of 1 total)

The topic ‘Display posts from multiple value in meta separated by comma’ is closed to new replies.