0

我正在运行以下查询:

$query = "SELECT post_title, ID, post_date_gmt, post_author
        FROM wp_posts 
            WHERE post_type='course-manager' 
        AND post_status='publish' 
        AND SUBSTRING(post_date_gmt,1,4) = '$this->yearGet'
        AND SUBSTRING(post_date_gmt,6,2) = '$this->monthGet'
        AND post_author = '$userid'
        ORDER BY post_title"; 
        $query_result = mysql_query ($query);
        while ($info = mysql_fetch_array($query_result))
        {

目前这只是查询 wordpress 帖子表。我还想从 wordpress postmeta 表中查询数据,一个例子是这样的,我在 postmeta 表中添加了“AND course_date:

 $query = "SELECT post_title, ID, post_date_gmt, post_author
        FROM wp_posts 
            WHERE post_type='course-manager' 
        AND post_status='publish' 
        AND SUBSTRING(post_date_gmt,1,4) = '$this->yearGet'
        AND SUBSTRING(post_date_gmt,6,2) = '$this->monthGet'
        AND post_author = '$userid'
            AND course_date = '$this->yearGet'
        ORDER BY post_title"; 
        $query_result = mysql_query ($query);
        while ($info = mysql_fetch_array($query_result))
        {

任何帮助将不胜感激

4

1 回答 1

0

如果您需要查询来自多个表的组合数据集,则需要进行联接。基本上,您需要在 2 个表之间建立连接,在 post 和 postmeta 的情况下,该连接将是 post id。所以查询应该是这样的:

SELECT a.something, b.anotherthing
FROM wp_posts AS a JOIN wp_postmeta AS b
ON a.ID = b.post_id
//followed by all of your where clause...
于 2012-08-24T14:55:58.740 回答