web-dev-qa-db-ja.com

分類条件による投稿リストの並べ替え

カスタム分類法からの用語で投稿のリストを並べるためのコードの書き方を疑問に思っていますか?

これは私がこれまでに持っているものです

  • カスタム投稿タイプのすべての投稿を1ページにまとめたページがあります。
  • 各CPT投稿の下に、表示するカスタム分類法を取得しました。
  • CPTの投稿をAlpha、ASC、DESCの順に並べ替えることを許可しました。

最後に、それはこのように見えます: enter image description here 

目標

私はすでに持っている "タイトル順"のものとのカップルリンクを追加したいと思います。

具体的には、(アルファ)ディレクター、スタジオ、エピソードの順に並べたいと思います。これらは文字列か数値のいずれかです。

私はまた年によって、シーズン初演で注文したいと思います、しかし、私はそれらの用語を命名する方法を変える必要があるかもしれません(それはおそらくそれが現在の方法でおそらく2011年冬、2012年冬を吐き出します)。

ジャンル別にソートすることも、複数の用語があるために取り組むつもりはないということです。

私はこれについてどのように取り組むべきかあまりよくわからず、助けを借りることができます。

ページの現在のコード

<div class="content-container">
<a href="?sort=titleup">Sort By Title A-Z</a>
<a href="?sort=titledown">Sort By Title Z-A</a>
<hr>

<?php
$type = 'animes';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);

if( isset( $_GET['sort'] ) && "titleup" == $_GET['sort'] ){
$args['orderby'] = 'title';
$args['order'] = 'ASC';
}

if( isset( $_GET['sort'] ) && "titledown" == $_GET['sort'] ){
$args['orderby'] = 'title';
$args['order'] = 'DESC';
}

$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div class="anime-title"><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?> Page"><?php the_title(); ?></a></div>

<br><span>Director:</span>
<?php 
$taxonomy = 'director';
// get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
// separator between links
$separator = ', ';

if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {

$term_ids = implode( ',' , $post_terms );
$terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids );
$terms = rtrim( trim( str_replace( '<br />',  $separator, $terms ) ), $separator );

// display post categories
echo  $terms;
}
?>

<br><span>Studio:</span>
<?php 
$taxonomy = 'studio';
// get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
// separator between links
$separator = ', ';

if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {

$term_ids = implode( ',' , $post_terms );
$terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids );
$terms = rtrim( trim( str_replace( '<br />',  $separator, $terms ) ), $separator );

// display post categories
echo  $terms;
}
?>

<br><span>Season Premiered:</span>
<?php 
$taxonomy = 'season';
// get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
// separator between links
$separator = ', ';

if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {

$term_ids = implode( ',' , $post_terms );
$terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids );
$terms = rtrim( trim( str_replace( '<br />',  $separator, $terms ) ), $separator );

// display post categories
echo  $terms;
}
?>

<br><span>Episodes:</span>
<?php 
$taxonomy = 'episodes';
// get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
// separator between links
$separator = ', ';

if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {

$term_ids = implode( ',' , $post_terms );
$terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids );
$terms = rtrim( trim( str_replace( '<br />',  $separator, $terms ) ), $separator );

// display post categories
echo  $terms;
}
 ?>


<br><span>Genres:</span>
<?php 
$taxonomy = 'genre';
// get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
// separator between links
$separator = ', ';

if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {

$term_ids = implode( ',' , $post_terms );
$terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids );
$terms = rtrim( trim( str_replace( '<br />',  $separator, $terms ) ), $separator );

// display post categories
echo  $terms;
}
?>
<hr>

<?php
endwhile;
}
wp_reset_query();  // Restore global post data stomped by the_post().
?>

その他の情報

番組のタイトルをクリックすると、番組の画像、再び表示されたカスタム分類法、およびその番組に関連する他のすべての投稿を表示する別のページに読者を誘導します(タグは番組の名前である)。 。たとえば、「Boys and Girls」という番組でタグ付けされたレビューまたはディスカッションの投稿がある場合は、このページに表示されます。

分類法の用語をクリックすると、読者はその用語に関連するすべてのショーを一覧表示するページに移動します。例えば、そのスタジオによって作られたすべてのショー、12のエピソードを持つすべてのショー、ジャンル「アクション」の下のすべてのショー。

これは私がこれをどう設定するかの全体的な構造に潜在的に疑問を投げかけることができる。私はWeb開発とワードプレスにとても慣れていないので、自分が行った調査に基づいて最善を尽くしました。

私はウェブサイトのこの部分を構成することについての詳細と質問にさらに行く他の未解決のStack Exchangeの投稿をいくつか持っています。また、私が試みようとしていることについてさらに説明が必要な場合は、そこにも感謝します。

カスタム投稿タイプと分類構造

分類法によるソート可能な表の作成

あなたの時間とあなたが皆さんにこれまで私に与えてくれた助けを本当にありがとうございました。

3
Nimara

WordPressコア(関係者によって複数の機会に表現される)は、用語によるソートを強く推奨しません。これは、用語を排他的に grouping メカニズムと見なしており、順序付け機能は含まれていません。

ですから、あなたの特定のケースでは、WPは、さまざまな監督がいること、それらの監督によって行われるショーのグループがあることを理解するでしょうが、ショーの順序がどうなるかについていくらか期待があることをまったく理解しません割り当てられた取締役の影響を受ける。

もちろん、実用的な開発では do さまざまな状況で用語でソートする必要があります。実際には、これはそれを実現するための非常にカスタムなSQLを意味します。

私が知っている実装の最も有用な例の1つは Sortable Taxonomy Columns ブログ記事でカバーされています。

そのためには、必要なSQLを記述/修正してからクエリに組み込む必要があります。

4
Rarst

Rarst's answer は正しい、有用であり、主題についての理解を深める余地を本当に与えますが、WP_Queryをcustom_post_metaで並べるための最も短い実用的な答えは次のとおりです。

$args = array(
    'meta_key' => 'name',  //custom field name here
    'orderby' => 'meta_value', 
    'order' => 'ASC') // the sort order
    // the rest of your arguments here...
);

ASCDESC以外のカスタムソート順が必要な場合は、 カスタムクエリ を構築する必要があります。


更新:私の答えは質問とは無関係です。誤解しているだけです。ごめんなさい。

コメントには、特定の属性にcustom_post_metataxonomyのどちらを使用するかをまだ決めていない人に役立つ情報が含まれているので削除しません。 Rarstが指摘したように、Scribuのクエリ(3番目の改良版)を使用して、分類用語によるソートが可能です。しかし、あなたはそれを避けるべきです、それはあなたの問い合わせのスピードに負担をかけるでしょうから。

グループ化するにはterms、ソートするにはmetaを使用してください。あなたのサイトは、たくさんのエントリがあっても、ずっと速くなるでしょう。

2