0

i have two post type "Artists" and "Songs", in "Songs" i have a meta box with checkbox list of "Artists" names.

i want to show related posts if any post have same value of post meta with artist name:

wp_reset_postdata();
global $post;
$artist_name = get_post_meta(get_the_ID(), "artist_name", true);

$args = array(
    'post_type' => array('songs'),
    'meta_key' => $artist_name
);

$query = new WP_Query;
return $query;

query should display posts with same value of meta box automatic.

this is possible ?!

more info:

post type of artists names:

Meta box of artists names in Songs post type

Showing related songs by artists name in single page

  • Have you tried it? Does it work? If not, what happens? By "query" I assume you mean WP_Query? Do you have debugging enabled? – s_ha_dum Apr 27 '14 at 14:33
  • yes i have WP_Query, it not working, i tried 'meta_key' => 'artist_name' but always it showing all posts and not from meta key. – Behzad G. Apr 27 '14 at 14:38
  • Could you show the rest of your code? I don't see any wp_query in here... Do you modify args later? – Krzysiek Dróżdż Apr 27 '14 at 14:44
  • $artist_name = get_post_meta(get_the_ID(), "artist_name", true); $args = array( 'post_type' => array('songs'), 'meta_key' => $artist_name, ); $query = new WP_Query; return $query; – Behzad G. Apr 27 '14 at 15:03
1

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
  }
}
  • i've tested your code and it's dont showing posts. just showing: array(1) { [71]=> string(2) "71" } – Behzad G. Apr 27 '14 at 15:02
  • Which block of code are you referring to? – s_ha_dum Apr 27 '14 at 15:05
  • second and latest code – Behzad G. Apr 27 '14 at 15:07
  • You should not get that result with any of my code. I tested each. The first and second block won't work. Those are to illustrate the problem. The third and fourth should. None of them should return that particular array. I don't know what you are doing but you are doing it wrong. – s_ha_dum Apr 27 '14 at 15:13
  • Third code is working :) tnx, it showing posts with same meta, Now: (mete_box is checkbox) my meta_box "artists_name" have different value example: Enrique, Rihanna, ... So i want when user are in post with Rihanna meta_box so related showing posts with this artist only. how should i change code? – Behzad G. Apr 27 '14 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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