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 9032527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:00:13+00:00 2026-06-16T08:00:13+00:00

I have two tables from target website in database and have to get post

  • 0

I have two tables from target website in database and have to get post thumbnails rows. One table is wp_posts and another is wp_postmeta. Standard WordPress export doesn’t include featured images so I intended to get it myself. wp_posts contains all posts, and featured image also is a kind of post and stored in this table. wp_postmeta table contains different metadata linked with post id, and value of _thumbnail_id key is the ID of post type ‘attachment’. The query below will clarify a little bit what I am trying to say.

Select * From wp_posts 
Where ID In (Select meta_value 
    From wp_postmeta 
    Where meta_key='_thumbnail_id' 
    And post_id In (Select ID From wp_posts 
        Where post_type='$post_type'));

And wp_post contains about 10k rows and wp_postmeta contains about 70k rows. Thanks for all kinda helping. Also will be great if you guys tell me how could I migrate specific post types with thumbnails from one wordpress installation to another.

  • 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-06-16T08:00:15+00:00Added an answer on June 16, 2026 at 8:00 am

    I think the nested IN clauses are likely the root of your performance problem. You need a moderately complicated query since WordPress 1) stores your post, which contains your post_type as one row in wp_posts, 2) the thumbnail ‘attachment’ as another row in wp_posts, and 3) the attachment metadata in wp_postmeta. I am sure that is why you where using the nested INs but INs are not especially efficient.

    Your query does work, but with a database the size of yours you are probably running out of memory or time, or both. On my DB, which is much smaller, your query executes in 0.0123 seconds. The following query, which exactly duplicates your query’s results, executes in 0.0006 seconds.

    SELECT children.*
    FROM wp_posts as children
    JOIN wp_posts as parents on parents.ID = children.post_parent
    WHERE children.ID IN (
        SELECT meta_value
        FROM wp_postmeta
        WHERE post_id = parents.ID
        AND meta_key='_thumbnail_id'
    ) 
    AND parents.post_type='{$post_type}'
    

    Given that it returns 4 rows from my DB, you can see what a huge execution difference that is.

    The following query is not much slower than the one above (0.0013 seconds) but returns rows not returned by the previous query (one extra in my case), and is still many times faster than yours. I think it is the query you want.

    SELECT *
    FROM wp_posts as children
    JOIN wp_posts as parents on parents.ID = children.post_parent
    JOIN wp_postmeta ON parents.ID = wp_postmeta.post_id
    AND wp_postmeta.meta_key = '_thumbnail_id'
    AND children.post_type = 'attachment'
    AND parents.post_type = '{$post_type}'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have two tables i am trying to get information from. login table -
I have two tables and their only difference is one column is missing from
I have two tables, one that has a foreign key from the other. I
I have a method that transfers data from one database to another and in
Very simplified, I have two tables Source and Target. declare @Source table (SourceID int
I have two tables: a source table and a target table. The target table
I need to copy data from one table to another. The tables do not
I have two different tables from which I need to pull data blogs which
I have this two tables from my program to manage stock: Articulos (items): id_articulo
I have two tables with list of urls fetched from different sources. I want

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.