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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:02:04+00:00 2026-06-02T12:02:04+00:00

I am trying to manually query from an external php file the wordpress database

  • 0

I am trying to manually query from an external php file the wordpress database and I want to extract the last 3 posts with: title, content, last (or first) image, and for that image, from wp_postmeta I want to get the thumbnail url.

I managed to get the title, content, image id, but I couldn’t figure it out how to add another join for getting image thumbnail. This is what I have:

SELECT a.post_title title, max(c.guid) img_url, a.ID id
FROM wp_posts a

LEFT JOIN
(select post_parent, max(post_date_gmt) as latest_image_date from wp_posts
where post_type='attachment' GROUP BY post_parent) b 
on a.id=b.post_parent

LEFT JOIN wp_posts c
on c.post_parent=a.id 
and c.post_type='attachment' 
and b.latest_image_date = c.post_date_gmt where c.guid IS NOT NULL

GROUP BY a.post_title ORDER BY a.ID

The image thumbnail is in wp_postmeta (meta_id, post_id, meta_key, meta_value) table, looking like this: 58435, 6711, _wp_attachment_metadata, a:6:{s:5:"width";s:4:"1024";s:6:"height";s:3:"683"...

I could see I get the image id in c.id and all it needs is another JOIN to also get data from wp_postmeta for the field where meta_key="_wp_attachment_metadata" and post_id=c.id.

Can anyone help me with completing the query? Thanks!

  • 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-02T12:02:05+00:00Added an answer on June 2, 2026 at 12:02 pm

    In the end I chose another solution (at least temporary) because it adds horizontal capabilities: I created a completely empty custom page (I had to manually remove some actions/filters to get it empty) in the other site (the one to be included) which exports data in json format. I get that data and print it as I need in the current site.

    <?php
    $args= array(
        'posts_per_page' => 6
        //if there are sticked posts they will also appear beside the 2 if we run this in a separate php file, not included in theme
    );
    $ii = 0;
    query_posts($args);
    if( have_posts() ) :
    
    
      while ( have_posts() ) : the_post();
    
        $permalink = get_permalink();
        $titlu = get_the_title();
        $excerpt = get_the_excerpt();
    
        $args = array(
            'post_type'      => 'attachment',
            'post_parent'    => $post->ID,
            'post_status'    => 'inherit',
            'numberposts'    => 1
        );
        $images = get_posts($args);
        $j = 1;
            foreach ($images as $i) :
    
            $ii++;//am preluat doar o poza, e ok numaratoarea
            $j++;
            $poza_thumb = wp_get_attachment_image_src($i->ID, 'thumbnail');
            $arr[$ii] = array('titlu' => $titlu, 'url' => $permalink, 'poza_thumb' => "".$poza_thumb[0], 'poza_width' => "".$poza_thumb[1], 'poza_height' => "". $poza_thumb[2], 'articol' => $excerpt);
    
            endforeach;
    
        endwhile;
    
        echo json_encode($arr);//we send json as array
    else :
    
    endif;?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a reference manually from one Model to another model, using
Environment: PHP 5.3 I'm trying to write my own query parameter substitution method. Basically
I'm trying to do this query INSERT INTO dbo.tbl_A_archive SELECT * FROM SERVER0031.DB.dbo.tbl_A but
I'm trying to export some data from MATLAB to a database. I use a
Im trying to call all users from a database with the same interests as
I'm having issues trying to extract utf-8 encoded values stored in an sqlite3 database
I am new to mySql and am trying to do a filtering query from
We are trying to copy a BO Query Service from one Universe to another.
I am trying to identify which posts on a wordpress installation contains empty paragraphs,
I'm trying to get a query from a specific model. I'm having trouble getting

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.