I get data from a table wp_posts and now I need to get data from the table wp_postmeta that match the ID captured in wp_posts table .

The data that I need to grab in the table wp_postmeta are product_img1 , product_img2 , product_img3 as shown in image below.

enter image description here

This is a view of my wp_posts:

enter image description here

I need to show ID, post_title, product_img1, product_img2, product_img3. Here's the SQL:

$show_info = $pdo("SELECT p.*, pm.*  
                   FROM wp_posts p  
                   JOIN wp_postmeta pm ON p.post_id = pm.meta_id 
                   WHERE p.post_type = 'wpcproduct'"

Question: Can you help me to show this data for ID 103?


With a WP_Query() you can have all of them what you want in an easy way. See all the arguments available in the Codex. With the following code, we are grabbing all(-1) the posts from wp_posts table, and using the post ID we are grabbing the postmeta items.

$productquery = new WP_Query( array( 'post_type'=>'wpcproduct','post_status'=>'publish','posts_per_page'=>-1 ) );

if( $productquery->have_posts() ) :
   while( $productquery->have_posts() ) : $productquery->the_post();
      global $post;
      $post_id = $post->ID;

      echo '<h2>'. $post_id .' - '. get_the_title() .'</h2>';
      echo '<img src="'. get_post_meta( $post_id, 'product_img1', TRUE ) . '" alt="Product Image 1">';
      echo '<img src="'. get_post_meta( $post_id, 'product_img2', TRUE ) . '" alt="Product Image 2">';
      echo '<img src="'. get_post_meta( $post_id, 'product_img3', TRUE ) . '" alt="Product Image 3">';

else :
   echo __( 'No Product\'s found', 'textdomain' );
wp_reset_postdata(); //reset the query after use
| improve this answer | |

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.