2012-12-13 39 views
1

我有一個頁面模板「通過作者」(ID 1814)我用來查詢我的帖子(標題:書名&內容:描述)meta_key:author_name,第一個& last name_但是,我想以作者的姓氏命令這些帖子 - meta_value中的第二個單詞。我能夠通過過濾得到這些結果的循環:wordpress命令的第二個字的meta_value

$values = get_post_custom_values("author_name"); 
$alpha = end(explode(' ',$values[0])); 

但我不知道如何獲得這些職位對於這個結果不是由AUTHOR_NAME的名字訂購。我發現下面的查詢爲functions.php文件,我認爲這使我在正確的路徑,但我使用它時,在我的頁面上返回一個404錯誤。

代碼從這裏:http://randyhoyt.com/wordpress/custom-post-type-ordering/

我還沒有加入SQL查詢爆炸(或者不管它是在SQL不是PHP)。不知道如果代碼不工作,因爲我在頁面模板上查詢而不是archive.php文件?任何建議讓我走上正確的道路?

add_filter('posts_join', 'author_join'); 
function author_join($wp_join) { 
    if(is_page(1814)) { 
     global $wpdb; 
     $wp_join .= " LEFT JOIN (
       SELECT post_id, meta_value as author_name 
       FROM $wpdb->postmeta 
       WHERE meta_key = 'author_name') AS DD 
       ON $wpdb->posts.ID = DD.post_id "; 
    } 
    return ($wp_join); 
} 

add_filter('posts_orderby', 'author_order'); 
function author_order($orderby) { 
    if(is_page(1814)) { 
      $orderby = " DD.post_title ASC"; 
    } 
    return $orderby; 
} 
+0

你在functions.php文件中添加此代碼或別的地方? –

回答

0

我已經經歷了類似的問題,即樂隊的名字開始與我實施了「......」是按字母順序排列在T.此修復程序是扭轉在數據庫中的名稱以滿足MySQL查詢,然後顯示頁面可以使用字符串函數將名稱反轉爲正確的順序。

像這樣的事情在顯示頁面上會固定輸出:

// The author's name, reverse ordered. 
$author_name = 'Doe,John'; 

// Break the full name into its parts. 
list($last_name,$first_name) = explode(',',$author_name); 

// Then display the name in the correct order. 
echo(first_name.' '.last_name); 
相關問題