0

How can i get meta_box foreach value inside meta_query value, here is code:

$my_id = get_post_meta(get_the_ID(), "artist_name", true);
$artistNames = array();  
foreach($my_id as $id) {
    $artistNames[] = $id;
}
$artists = implode($artistNames,", ");

$args = array(
    'post_type' => array ( 'songs', 'videos' ),
    'meta_query' => array(
        array(
            'key' => 'artist_name',
            'value' => array( $artists )
        )
    )
);

$query = new WP_Query($args);

do this possible ?!

1 Answer 1

0

Don't implode $artistNames. The value meta_query argument will take an array if you use any of several compare arguments. You need IN, I believe.

value (string|array) - Custom field value. It can be an array only when compare is 'IN', 'NOT IN', 'BETWEEN', or 'NOT BETWEEN'....

http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

Assuming that $artistNames is an array of ids you can use it directly.

$args = array(
    'post_type' => array ( 'songs', 'videos' ),
    'meta_query' => array(
        array(
            'key' => 'artist_name',
            'value' => $artistNames,
            'compare' => 'IN'
        )
    )
);

And in fact, it looks like you can skip that first foreach that generates $artistNames and just use $my_id.

$args = array(
    'post_type' => array ( 'songs', 'videos' ),
    'meta_query' => array(
        array(
            'key' => 'artist_name',
            'value' => $my_id,
            'compare' => 'IN'
        )
    )
);
2
  • $my_id is checkbox with multiple value, how can i get multiple value from "get_meta_box" for "meta_query value" ?
    – Behzad G.
    Commented Apr 28, 2014 at 10:19
  • get_meta_value(), as in your question, but the third parameter is a singular argument. If you've saved an array (as your code implies) what I posted above should work. If you have saved individual key/value pairs you need the third parameter to be false, not true. Is that the question?
    – s_ha_dum
    Commented Apr 28, 2014 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.