0

I am trying to display some meta_values and meta_keys of found post_ids from wp_postmeta table and the post_title of this ID from wp_posts table. Firstly I have to find post_ids (it could be more than one) from given meta_key and meta_value. Than I have to retrieve other meta_values and meta_keys related to post_ids in a table. I have managed to retrieve post_ids with below code;

$meta_key = "wcum_users_id"; 
$meta_value = "2";

$posts = $wpdb->get_results("SELECT * FROM wp_postmeta WHERE meta_key = '$meta_key' AND meta_value = '$meta_value' ");
foreach ( $posts as $post ){

$id = $post->post_id;

echo $id;

And it shows;
3873
3797
for example. Then I add the below code inside foreach;

$the_query = new WP_Query( array( 'post_id' => '$id', 'post_type' => 'custom' ) );
// The Loop

while ( $the_query->have_posts() ) : $the_query->the_post();

echo '<li>';
the_title();
echo '</li>';
endwhile;

and it results;
3783
trl2000
hgld2015
3797
trl2000
hgld2015

But I need a structure like that;

Title | meta_key1 | meta_key2 | meta_key3
$post_id1 | meta_value11 | meta_value12 | meta_value13
$post_id2 | meta_value21 | meta_value22 | meta_value23

Is it possible?

Berk Koç
  • 15
  • 8
  • 2
    I don't understand what the issue is...Why not just use [get_post_meta()](https://developer.wordpress.org/reference/functions/get_post_meta/) in your loop? – rnevius Aug 20 '15 at 15:31
  • @rnevius I tried to use $meta = get_post_meta( $id ); but it return array inside the post_id values. – Berk Koç Aug 20 '15 at 20:32

0 Answers0