Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3351350
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:53:53+00:00 2026-05-18T01:53:53+00:00

I have a WP site where I’ve also got a few php pages outside

  • 0

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

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.. .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T01:53:54+00:00Added an answer on May 18, 2026 at 1:53 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.