2011-02-06 28 views
0

我需要加入與自定義表和POSTMETA POSTS。我跟蹤自定義表中的熱門帖子,但我只希望返回具有特定帖子值的帖子。Wordpress查詢內部加入職位 - >自定義表 - >後meta元其中metakey =元值

我搜索了,找不到嘖嘖。

以下是我認爲我應該做的......但在phpmyadmin中手工完成時不起作用。

SELECT(POST信息)FROM帖p INNER JOIN custom_table T ON p.ID = t.ID INNER JOIN post_meta米ON p.ID = m.ID WHERE m.metakey = '的myKey' AND post_type = '後' AND POST_DATE < '$現在' AND POST_DATE> '$ lastmonth' ORDER BY postcount DESC LIMIT 5" );

我需要內部連接後元作爲一個獨立的子查詢?

回答

2

如果我可能會建議,請嘗試使用WP_Query()。它會有點笨拙,因爲您需要爲發佈日期範圍添加一個過濾器,然後將其刪除,但是否則它可能會在沒有三層SQL聯接的情況下發揮預測功能。

<?php 
include_once("wp-config.php"); 

function filter_date_range($where = '') { 
    $lastmonth = date("Y-m-d 00:00:00", strtotime("-1 month")); 
    $where .= " and post_date<now() and post_date>'{$lastmonth}'"; 
    return($where); 
} 

add_filter('posts_where', 'filter_date_range'); 

$q = new WP_Query(array(
    "post_status" => "publish", 
    "post_type" => "post", 
    "posts_per_page" => 5, 
    "meta_query" => array(array(
     "key" => "mykey", 
     "value" => "my_preferred_value" 
    )) 
)); 

remove_filter('filter_date_range'); 

var_dump($q->posts); 

?>