I am assuming that what you are trying to do is something like this:
$artist_name = get_post_meta(get_the_ID(), "artist_name", true);
$args = array(
'post_type' => array('songs'),
'meta_key' => $artist_name,
);
$related = new WP_Query($args);
In your question you state that you want posts that “have same value of post meta with artist name”. What the code above does is not search for values associated with the key but for key names matching the artist. Try:
$artist_name = get_post_meta(get_the_ID(), "artist_name", true);
var_dump($artist_name);
$args = array(
'post_type' => array('songs'),
'meta_key' => $artist_name,
);
$related = new WP_Query($args);
var_dump($related->request);
And you should see what is happening.
What you want instead is something like this:
$artist_name = get_post_meta(get_the_ID(), "artist_name", true);
$args = array(
'post_type' => array('songs'),
'meta_key' => 'artist_name',
'meta_value' => $artist_name,
);
$related = new WP_Query($args);
Or a more complex but more flexible meta_query
:
$artist_name = get_post_meta(get_the_ID(), "artist_name", true);
$args = array(
'post_type' => array('songs'),
'meta_query' => array(
array(
'key' => 'artist_name',
'value' => $artist_name,
)
)
);
$related = new WP_Query($args);
I’d lean toward the meta_query
as it nicely encapsulates the related “meta” arguments, and allows for more options.
You will have to loop over the result to return anything useful, of course. For example:
if ($related->have_posts()) {
while ($related->have_posts()) {
$related->the_post();
the_title();
// etc
}
}