questions

WordPress metadata array output

Rate this post

I’m trying to show some related products under a blog post (consumed from meta data). The meta data is saved, and can be echo’ed in the blog post. When I use a WP_Query to show the name/image of the product it doesn’t do anything.

I’m saving the related products as a string ‘8718699784751’, ‘8718699784751’. and fetch it via:

$ean = get_post_meta($post->ID, 'related', true);

When I echo the meta-data it shows the correct info:

<?php echo $ean;?>

Now I want to use the same data in a WP_query, which doesn’t work:

<?php
 $args = array(
                            'post_type'      => 'page',
                            'meta_key'       => 'ean',
                            'meta_value'     => array($ean),
                            'posts_per_page' => -1,
                            'order'          => 'ASC',
                            'orderby'        => 'menu_order'
                         );

                        $parent = new WP_Query( $args );

                        if ( $parent->have_posts() ) : ?>

When I hard code the product numbers in the WP_Query it does work:

<?php

                        $args = array(
                            'post_type'      => 'page',
                            'meta_key'       => 'ean',
                            'meta_value'     => array('8718699784751', '8718699784751'),
                            'posts_per_page' => -1,
                            'order'          => 'ASC',
                            'orderby'        => 'menu_order'
                         );

                        $parent = new WP_Query( $args );

                        if ( $parent->have_posts() ) : ?>

What am I doing wrong?

Update; When I save meta data as 8718699784775 (without ‘,’) it works, when I Use (‘8718699784775’, ‘8718699784799’) it does not.

 

✔️Solution:

I changed a few things.

  1. I was using Multibox.io plugin (didn’t recalled I did before!), so changed the implementation. I changed my meta box to ‘cloned’, which means I could input multiple values (every EAN has it’s own value).
  2. I used the official way of displaying meta data and placed it in an variable

$products = rwmb_meta( ‘related’ );

Now I was able to query on the $products variable with:

'meta_value'     =>  $products,

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button