MYSQL Statement INNER JOIN with multiple condition

I have a WP site where I’ve also got a few php pages outside of my WP installation which I need to query the database that powers my WP site. I have a working query but it doesn’t show what I really need output.

These are the two tables wp_posts and wp_postmeta

Read More
wp_posts
ID
post_author
post_date
post_date_gmt
post_content
post_title
post_excerpt
post_status
comment_status
pint_status
post_password
post_name
to_ping
pinged
post_modified
post_modified_gmt
post_content_filtered
post_parent
guid
menu_order
post_type
post_mine_type
comment_count

wp_postmeta
meta_id
post_id
meta_key
meta_value

and these is the query that show the publish post:

$query = "SELECT post_title, post_name FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 5";

and these is the query that show with the meta_key= ‘_wp_attached_file’

$query ="SELECT post.ID, post.post_title, post.post_excerpt, post.post_content, meta.meta_value FROM wp_posts AS post INNER JOIN wp_postmeta AS meta ON meta.post_id = post.id AND meta.meta_key = '_wp_attached_file' ";

what I want is to combine these two query to show post_status =’publish’ and the meta_key = ‘_wp_attached_file’

here is the example of my query but doesn’t show value

<?php
   $query ="SELECT post.ID, post.post_title, post.post_excerpt, post.post_content, meta.meta_value FROM wp_posts AS post INNER JOIN wp_postmeta AS meta ON meta.post_id = post.id AND meta.meta_key = '_wp_attached_file' WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 5 ";

    $result = mysql_query($query,$link);
        if(mysql_num_rows($result)) {
          while($post = mysql_fetch_object($result)) {
               echo "$post->meta_value";  
               echo "$post->post_title";
               echo "$post->post_content";
        }
        }
?>

many thanks guys.. .

another problem it show only 1 post and does not latest post

here is my updated code

<?php
$con = mysql_connect("localhost", "root", "");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

$db_selected = mysql_select_db("wp_db",$con);
$sql = "SELECT 
  posts.ID,
  posts.post_title AS title,
  posts.post_content AS content,
  files.meta_value AS filepath
FROM
  wp_posts posts
  INNER JOIN wp_posts attachments ON posts.ID = attachments.post_parent
  INNER JOIN wp_postmeta files ON attachments.ID = files.post_id
WHERE 1 = 1
  AND files.meta_key = '_wp_attached_file'";
$result = mysql_query($sql,$con);

while ($row = mysql_fetch_object($result))
  {
  echo $row->title . "<br />";
  }

mysql_close($con);
?>

do I get something wrong with my code.. .thank for help thank you so much.. .

Related posts

Leave a Reply

3 comments

  1. Hi @idontknowhow:

    Your problem appears to be invalid assumptions regarding WordPress’ database schema. WordPress stores it’s attachments as a post_type='attachment'. Run this query to see what I mean:

    SELECT 
      wp_posts.ID,
      wp_posts.post_type
    FROM
      wp_posts
      INNER JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
    WHERE 1 = 1
      AND wp_postmeta.meta_key = '_wp_attached_file'
    

    Records in wp_posts with a post_type='attachment' will typically have 'post_status='inherit' and ‘post_parent‘ equal to your post’s ID field value.

    Now I didn’t completely follow what you were looking to accomplish. You mentioned the technical problems you were having and but not the actual result you were trying to achieve. Without knowing the latter it’s hard for me to verify that I understood your question. So I may have misunderstood what you were looking for, but I think this is what you wanted:

    SELECT 
      posts.ID,
      posts.post_title AS title,
      posts.post_content AS content,
      files.meta_value AS filepath
    FROM
      wp_posts posts
      INNER JOIN wp_posts attachments ON posts.ID = attachments.post_parent
      INNER JOIN wp_postmeta files ON attachments.ID = files.post_id
    WHERE 1 = 1
      AND files.meta_key = '_wp_attached_file'
    

    But using direct SQL is frowned upon in the WordPress world; instead use of the WordPress API is the preferred choice if it provides what you need. There are many reasons for this; i.e. to avoid breakage if future WordPress versions change the database schema and because WordPress does a lot of data caching so it might be more performant in everyday usage to not use direct SQL and to use the WordPress API instead.

    However, it is not 100% straightforward to use the WordPress API to address your question, though it can be done. If you’d like to see how may I suggest asking over at StackOverflow’s sister site WordPress Answers where lots of WordPress enthusiasts can help?

    Hope this helps.

    -Mike

    P.S. I find that a lot of questions about querying the WordPress database here on StackOverflow have people attempt to answer them even though they lack knowledge of the WordPress database schema and more importantly, are not aware of the WordPress API. People here know MySQL really well but often don’t know WordPress well enough to give the right answer on WordPress-related questions. WordPress Answers is probably a better place to ask questions about WordPress.

  2. Did you tryed to filter mete.meta_key on WHERE? Something like that:

    SELECT post.ID, post.post_title, post.post_excerpt,
        post.post_content, meta.meta_value
    FROM wp_posts AS post
        INNER JOIN wp_postmeta AS meta ON meta.post_id = post.id
    WHERE meta.meta_key = '_wp_attached_file'
        AND post.post_type = 'post'
        AND post.post_status = 'publish'
    ORDER BY post.post_date DESC
    LIMIT 5;