11

Wordpress の分類法からすべての投稿を取得する方法はありますか?

にはtaxonomy.php、現在の用語に関連する用語から投稿を取得するこのコードがあります。

$current_query = $wp_query->query_vars;
query_posts( array( $current_query['taxonomy'] => $current_query['term'], 'showposts' => 10 ) );

用語に関係なく、タクソノミーのすべての投稿を含むページを作成したいと思います。

これを行う簡単な方法はありますか、または用語の分類法を照会してからループする必要がありますか?

4

4 に答える 4

13

@PaBLoXは非常に優れたソリューションを作成しましたが、私は自分でソリューションを作成しましたが、これは少しトリッキーで、用語ごとに毎回すべての投稿を照会する必要はありません。1 つの投稿に複数の用語が割り当てられている場合はどうなりますか? 同じ投稿を複数回レンダリングしませんか?

 <?php
     $taxonomy = 'my_taxonomy'; // this is the name of the taxonomy
     $terms = get_terms($taxonomy);
     $args = array(
        'post_type' => 'post',
        'tax_query' => array(
                    array(
                        'taxonomy' => 'updates',
                        'field' => 'slug',
                        'terms' => wp_list_pluck($terms,'slug')
                    )
                )
        );

     $my_query = new WP_Query( $args );
     if($my_query->have_posts()) :
         while ($my_query->have_posts()) : $my_query->the_post();

              // do what you want to do with the queried posts

          endwhile;
     endif;
  ?>

wp_list_pluck

于 2013-05-29T15:13:48.157 に答える
12
$myterms = get_terms('taxonomy-name', 'orderby=none&hide_empty');    
echo  $myterms[0]->name;

foreachこれで、最初のアイテムを投稿すると、次に;を作成できます。ループ:

foreach ($myterms as $term) { ?>
    <li><a href="<?php echo $term->slug; ?>"><?php echo $term->name; ?></a></li> <?php
} ?>

そうすれば、それらをすべて投稿したい場合、それらをリストすることができます-私の解決策-foreach内に通常のwordpressループを作成しますが、次のようなものが必要です:

foreach ($myterms as $term) :

$args = array(
    'tax_query' => array(
        array(
            $term->slug
        )
    )
);

//  assigning variables to the loop
global $wp_query;
$wp_query = new WP_Query($args);

// starting loop
while ($wp_query->have_posts()) : $wp_query->the_post();

the_title();
blabla....

endwhile;

endforeach;

ここに非常によく似たものを投稿しました。

于 2010-09-27T06:09:10.980 に答える
0

用語のクエリ ループ中に、すべての投稿参照を配列に収集し、後でそれを新しい WP_Query で使用できます。

$post__in = array(); 
while ( $terms_query->have_posts() ) : $terms_query->the_post();
    // Collect posts by reference for each term
    $post__in[] = get_the_ID();
endwhile;

...

$args = array();
$args['post__in'] = $post__in;
$args['orderby'] = 'post__in';
$other_query = new WP_Query( $args );
于 2015-07-31T13:05:29.233 に答える