1

I thought this would be simple, but cant figure it out or find any reference online. Basically Im trying to get posts who have the same meta value for a specific key as the current post. Heres what I have so far

global $authordata, $post;
$authors_posts = get_posts( array(
'author' => $authordata->ID,
'post_parent' => 0,
'orderby' => 'menu_order',
'post_type' => 'page',
'posts_per_page' => -1,
'meta_query' => array(
 array(
  'key' => 'stashkey',
  'value' => $id,
'compare' => '=' )
)
) );

This works fine to get all posts who have a meta value which equals the same as the current post id - but I want to get it based on having the same meta value as the current post.

Thanks

1 Answer 1

0

You should first get the meta value for the current post, then use it to make another query.

global $authordata, $post;
$my_meta = get_post_meta( $id, 'stashkey', true);
$authors_posts = get_posts( array(
'author' => $authordata->ID,
'post_parent' => 0,
'orderby' => 'menu_order',
'post_type' => 'page',
'posts_per_page' => -1,
'meta_query' => array(
 array(
  'key' => 'stashkey',
  'value' => $my_meta,
'compare' => '=' )
)
) );

This will fetch the posts that have equal stashkey meta as current post.

Your Answer

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

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