Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In my wp_postmeta table, I have the following values:

enter image description here

I'm trying query the posts whose ID lie in the meta_value column associated to a given post (the current post being viewed).

For example, lets suppose I'm viewing post 94. As illustrated above I would like to retrieve the posts 108,78,90 associated with 94 and display their titles.

In this way the an area (post type) is associated with a post by its ID being one of the meta values for the post's 'area' meta key.

I start with this:

$args = array(
          'post_type' => 'area',
          'posts_per_page' => -1,
      );

      $areas = new WP_Query($args);

But how I can specify I want the posts associated with the post 94?

share|improve this question
3  
Can you add some more explanation to you question? If possible add examples for better explanation here –  Maruti Mohanty Nov 6 '13 at 14:53
2  
I'm sorry, but it is really unclear what you're trying to do here. –  Chip Bennett Nov 6 '13 at 15:00
    
the screenshot is saying areas, but the query is written with area - is that correct? –  Mayeenul Islam Nov 6 '13 at 15:02
    
Might be overcomplicating things, but connecting post types to post types might be a good job for Posts 2 Posts –  helgatheviking Nov 6 '13 at 15:17
    
Hi @marcelo2605 if you have follow up questions, please create a new one. –  Stephen Harris Nov 6 '13 at 15:48

1 Answer 1

So you want the values 108,78,90 corresponding to the meta key 'areas' for the post with ID 94?

First retrieve those IDs:

$the_post_id = 94;
$area_ids = get_post_meta( $the_post_id, 'areas', false );

//Make sure they're all positive integers
$area_ids = array_map( 'absint', $area_ids );

if( $area_ids ){
   //We have IDs, retrieve posts
   $areas = get_posts( array(
      'post_type' => 'area',
      'numberposts' => -1,
      'post__in' => $area_ids;
   ));

   if( $areas ){
       //We have areas
       foreach( $areas as $area ){
          echo get_the_title( $area ). '<br/>';
       }
   }

}

The above has not been tested, but should work in theory. If you want $the_post_id to the be ID of the post currently being viewed then you can use get_the_ID().

share|improve this answer
    
+1. Beat me to it. –  Chip Bennett Nov 6 '13 at 15:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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