Leave a Reply

3 comments

  1. If I understand your question correctly, this should work:

    function get_event_info($event_id = 0, $info = '' ) {
      global $wpdb;
      return $wpdb->get_col(
        $wpdb->prepare(
          "SELECT meta_value from $wpdb->postmeta WHERE 
          meta_key = '%s' AND post_id IN
          (SELECT DISTINCT post_id FROM $wpdb->postmeta WHERE meta_key = 'event_id' AND meta_value= %s )",
          $info, $event_id
        )
      );
    }
    

    Use this like so:

    $names = get_event_info( 19 , 'Name' );
    if ( ! empty($names) ) {
      echo '<ul>';
      foreach ($names as $name) {
        echo '<li>' . $name . '</li>';
      }
      echo '</ul>';
    }
    

    Please be sure the second parameter is exactly the same as the meta_key: in your example meta_key = 'Name' but the SQL query is WHERE meta_key='name', case is different!

  2. You can select your posts by using the meta_query parameter in the WP_Query class. Get all the post ID’s of the posts who have a shared event_id of 19.

    You can use the ID’s later on to get all necessary data.

  3. I stumbled in the same problem and get more or less what I wanted in this way:

    SELECT t.post_id, t.meta_value, u.meta_value
    FROM  `wp_postmeta` t INNER JOIN  `wp_postmeta` u
    ON t.post_id = u.post_id
    WHERE
    u.meta_key =  'first_key' AND
    t.meta_key =  'second_key'