1

解決済み: 解決コードは次のとおりです。

//Extend Category queries to support "latest_post" for orderby parameter
function filter_term_sort_by_latest_post_clauses( $pieces, $taxonomies, $args )
{
    global $wpdb;
    if ( in_array('category', $taxonomies) && $args['orderby'] == 'latest_post' )
    {
        $pieces['fields'] .= ", MAX(p.post_date) AS last_date";
        $pieces['join'] .= " JOIN $wpdb->term_relationships AS tr JOIN $wpdb->posts AS p ON p.ID=tr.object_id AND tr.term_taxonomy_id=tt.term_taxonomy_id";
        $pieces['where'] .= " AND p.post_status='publish' GROUP BY t.term_id";
        $pieces['orderby'] = "ORDER BY last_date";
        $pieces['order'] = "DESC"; // DESC or ASC
    }
    return $pieces;
}
add_filter('terms_clauses', 'filter_term_sort_by_latest_post_clauses', 10, 3);

元の質問:

WordPress サイトに次の関数とフィルター フックを追加しました。これにより、各カテゴリの最新の投稿で並べ替えられたカテゴリを一覧表示できます。この関数は、下書きの投稿が含まれていることを除いて、期待どおりに機能し、その特定のカテゴリがリストの一番上に移動します。

//Extend Category queries to support "latest_post" for orderby parameter
function filter_term_sort_by_latest_post_clauses( $pieces, $taxonomies, $args )
{
    global $wpdb;
    if ( in_array('category', $taxonomies) && $args['orderby'] == 'latest_post' )
    {
        $pieces['fields'] .= ", MAX(p.post_date) AS last_date";
        $pieces['join'] .= " JOIN $wpdb->term_relationships AS tr JOIN $wpdb->posts AS p ON p.ID=tr.object_id AND tr.term_taxonomy_id=tt.term_taxonomy_id";
        $pieces['where'] .= " GROUP BY t.term_id";
        $pieces['orderby'] = "ORDER BY last_date";
        $pieces['order'] = "DESC"; // DESC or ASC
    }
    return $pieces;
}
add_filter('terms_clauses', 'filter_term_sort_by_latest_post_clauses', 10, 3);

選択句「MAX(p.post_date) AS last_date」に公開された投稿の値のみを含めたい( WHERE p.post_status=publish" )

どうすればこれを達成できますか?

ありがとう!

4

1 に答える 1

0

$pieces['where'] .= " WHERE p.post_status='publish' GROUP BY t.term_id"; ステートメントを where 句に追加するのはどうでしょうか。. 何かを見逃していない限り、これは簡単に修正できるはずです。お役に立てれば。

于 2013-08-26T19:13:23.533 に答える