开发者

SQL function for data retrieval JOIN (second attempt)

开发者 https://www.devze.com 2023-03-04 16:24 出处:网络
Trying to write an SQL function that collects data from two different tables. It should get the current post id from wp_posts and then find the corresponding images from a table called wp_postdata.

Trying to write an SQL function that collects data from two different tables. It should get the current post id from wp_posts and then find the corresponding images from a table called wp_postdata.

Table 1

------------------------------           
id   ||   title   ||  content
------------------------------

Table 2

---------------------------------------------------------
meta_id   ||   post_id   ||   meta_key   ||   meta_value
---------------------------------------------------------

The meta_key that we are after is called _wp_attachment_metadata

My failed attempt ::

<?php
$pics = mysql_query("SELECT    id,  
           meta_key_wp_attached_file.meta_value picture 
 FROM      wp_posts
 LEFT JOIN wp_postmeta meta_key_wp_attached_file 
 ON        meta_key_wp_attached_file.post_id = wp_posts.id 
 WHERE     meta_key_wp_attached_file.meta_key = 'picture' 
    ORDER BY picture;");

while ($row = mysql_fetch_array($pics)) {
    $thumb = $row['picture'];
    echo '<img src="http://www.golfb开发者_开发百科rowser.com/wp-content/uploads/'.$thumb.'" />';

}

    ?>

Any ideas,

Marvellous


Should be: meta_key = '_wp_attachment_metadata', and you'll need to unserialize the result.

Or you can use the API:

http://codex.wordpress.org/Function_Reference/WP_Query

http://codex.wordpress.org/Function_Reference/get_post_meta

0

精彩评论

暂无评论...
验证码 换一张
取 消