开发者

Sql query to show some results using PHP

at the end of this I want to display the TITLES of the results that have extra1 as meta_key and test as meta_value.

My goal is to:

  1. From table wp_postmeta get the post_id of every row that has extra1 as meta_key and test开发者_如何转开发 as meta_value
  2. Using the post_id, get the post_title for every row on step 1 respectively.
  3. Print the post_title

Thank you very much.


this is the query assuming you are working on wordpress database schema:

SELECT post_title
FROM wp_posts
WHERE ID IN
(
   SELECT DISTINCT post_id
   FROM wp_postmeta
   WHERE meta_key = 'extra1' AND meta_value = 'test'
)

second query after Mairy's comment:

SELECT post_title
FROM wp_posts
WHERE ID IN
(
   SELECT DISTINCT post_id
   FROM wp_postmeta
   WHERE meta_key IN('extra1','extra2','extra3') AND meta_value IN('test','test1','test2','test3')
)

then you just need to loop over th result set with php and then print the post title as you prefer:

//connect to DB then ($query contains the query above)
$res = mysql_query($query, $db);
while($row=mysql_fetch_array($res))
{
   echo($row['post_title']);
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜