php 从 Wordpress 中的自定义分类法中获取所有帖子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3354272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:27:20  来源:igfitidea点击:

Get all posts from custom taxonomy in Wordpress

phpwordpresstaxonomy

提问by Andrei

Is there a way to get all the posts from a taxonomy in Wordpress ?

有没有办法从 Wordpress 的分类法中获取所有帖子?

In taxonomy.php, I have this code that gets the posts from the term related to the current term.

在 中taxonomy.php,我有这个代码可以从与当前术语相关的术语中获取帖子。

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

I'd like to create a page with all the posts in the taxonomy, regardless of the term.

我想创建一个包含分类法中所有帖子的页面,无论术语如何。

Is there a simple way to do this, or do I have to query the taxonomy for the terms, then loop trough them, etc.

有没有一种简单的方法可以做到这一点,或者我是否必须查询术语的分类法,然后循环遍历它们等。

采纳答案by Pablo Olmos de Aguilera C.

$myterms = get_terms('taxonomy-name', 'orderby=none&hide_empty');    
echo  $myterms[0]->name;

With that you'd post the first item, yo can then create a foreach; loop:

有了它,您将发布第一项,然后您可以创建一个foreach; 环形:

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

That way you'd list them, if you want to post all of them, -my solution- create a normal wordpress loop inside the foreach one, but it has to have something like:

这样你就可以列出它们,如果你想发布所有这些,-我的解决方案-在 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;

I posted something very similar here.

我在这里发布了非常相似的内容。

回答by maksbd19

@PaBLoX made a very nice solution but I made a solution myself what is little tricky and doesn't need to query for all the posts every time for each of the terms. and what if there are more than one term assigned in a single post? Won't it render same post multiple times?

@PaBLoX 提出了一个非常好的解决方案,但我自己做了一个解决方案,有点棘手,不需要每次都为每个术语查询所有帖子。如果在一个职位中分配了多个任期怎么办?它不会多次呈现相同的帖子吗?

 <?php
     $taxonomy = 'my_taxonomy'; // this is the name of the taxonomy
     $terms = get_terms( $taxonomy, 'orderby=count&hide_empty=1' ); // for more details refer to codex please.
     $args = array(
        'post_type' => 'post',
        'tax_query' => array(
                    array(
                        'taxonomy' => 'updates',
                        'field' => 'slug',
                        'terms' => m_explode($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;
  ?>

this function m_explodeis a custom function which i put into the functions.phpfile.

这个函数m_explode是我放入functions.php文件的自定义函数。

    function m_explode(array $array,$key = ''){     
        if( !is_array($array) or $key == '')
             return;        
        $output = array();

        foreach( $array as $v ){        
            if( !is_object($v) ){
                return;
            }
            $output[] = $v->$key;

        }

        return $output;

      }

UPDATE

更新

We don't require this custom m_explodefunction. wp_list_pluck()function does exactly the same. So we can simply replace m_explodewith wp_list_pluck()(parameters would be same). DRY, right?

我们不需要这个自定义m_explode函数。wp_list_pluck()功能完全一样。所以我们可以简单地替换m_explodewp_list_pluck()(参数相同)。干,对吧?

回答by sun

Unlike for post types, WordPress does not have a route for the taxonomy slug itself.

与帖子类型不同,WordPress 没有分类 slug 本身的路径。

To make the taxonomy slug itself list all posts that have any term of the taxonomy assigned, you need to use the EXISTSoperator of tax_queryin WP_Query:

要使分类 slug 本身列出分配了任何分类术语的所有帖子,您需要使用inEXISTS运算符tax_queryWP_Query

// Register a taxonomy 'location' with slug '/location'.
register_taxonomy('location', ['post'], [
  'labels' => [
    'name' => _x('Locations', 'taxonomy', 'mydomain'),
    'singular_name' => _x('Location', 'taxonomy', 'mydomain'),
    'add_new_item' => _x('Add New Location', 'taxonomy', 'mydomain'),
  ],
  'public' => TRUE,
  'query_var' => TRUE,
  'rewrite' => [
    'slug' => 'location',
  ],
]);

// Register the path '/location' as a known route.
add_rewrite_rule('^location/?$', 'index.php?taxonomy=location', 'top');

// Use the EXISTS operator to find all posts that are
// associated with any term of the taxonomy.
add_action('pre_get_posts', 'pre_get_posts');
function pre_get_posts(\WP_Query $query) {
  if (is_admin()) {
    return;
  }
  if ($query->is_main_query() && $query->query === ['taxonomy' => 'location']) {
    $query->set('tax_query', [
      [   
        'taxonomy' => 'location',
        'operator' => 'EXISTS',
      ],  
    ]);
    // Announce this custom route as a taxonomy listing page
    // to the theme layer.
    $query->is_front_page = FALSE;
    $query->is_home = FALSE;
    $query->is_tax = TRUE;
    $query->is_archive = TRUE;
  }
}

回答by Jens

While in the query loop for terms, you can collect all post references in an array and use that later in a new WP_Query.

在术语的查询循环中,您可以收集数组中的所有帖子引用,然后在新的 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 );