简体   繁体   English

Wordpress:即使术语有对象,get_terms() 也不返回任何内容

[英]Wordpress: get_terms() not returning anything even if terms have objects

Normal behaviour of get_terms is not to return terms if there are no posts assigned to.如果没有分配给任何职位,get_terms 的正常行为是不返回术语。 But this is not the case, I can see terms assigned in admin and also checked the database and all seems fine.但事实并非如此,我可以看到 admin 中分配的术语并检查了数据库,一切似乎都很好。 Also check this code:还要检查此代码:

$p = get_post(5018); // correctly returns the post

// works: returns the assigned term
$post_terms = wp_get_post_terms($p->ID, 'solutions_sectors', array("fields" => "all"));

// now the opposite:
$first = $post_terms[0];
$tid = $first->term_id;
// works: gives a list of post ids
$term_posts = get_objects_in_term($tid, 'solutions_sectors');

// still, this will output an empty array:
$terms = get_terms(array('taxonomy' => 'solutions_sectors');

// while this will output the right array (obviously):
$terms = get_terms(array('taxonomy' => 'solutions_sectors', 'hide_empty' => false));

So, my posts do have the terms, but get_terms seems not to realise it.所以,我的帖子确实有条款,但 get_terms 似乎没有意识到这一点。 Why?为什么?

Please note the following:请注意以下事项:

  • I'm using custom post types with custom taxonomies我正在使用带有自定义分类法的自定义帖子类型

  • I'm using polylang as the languages plugin (but all the posts and terms seems to be correctly translated and assigned)我使用 polylang 作为语言插件(但所有帖子和术语似乎都正确翻译和分配)

Found the problem: the count field of the term_taxonomy table was empty, and this is because I bulk-saved my posts using wp_insert_post() during a custom import.发现问题:term_taxonomy 表的count 字段为空,这是因为我在自定义导入期间使用wp_insert_post()批量保存了我的帖子。

wp_insert_post() seems to have a bug: it correctly applies specified terms to the new post but doesn't update the term_taxonomy count. wp_insert_post()似乎有一个错误:它正确地将指定的术语应用于新帖子,但不更新 term_taxonomy 计数。

The solutions here is a one-shot call to wp_update_term_count_now()`.这里的解决方案是对 wp_update_term_count_now()` 的一次性调用。

Since I have to retrieve all the terms ids on a file executed prior to the taxonomies creation, I've got to wrap the code in an init action.由于我必须检索在创建分类法之前执行的文件上的所有术语 ID,因此我必须将代码包装在 init 操作中。

add_action('init','reset_counts', 11, 0);
function reset_counts(){
  // I'm currently using polylang so first I get all the languages
  $lang_slugs = pll_languages_list(array('fields' => 'slug'));

  foreach($lang_slugs as $lang){
    $terms_ids = get_terms(array(
      'taxonomy' => 'solutions_sectors'
      ,'fields' => 'ids'
      ,'lang' => $lang
      ,'hide_empty' => false
    ));

    // it's important to perform the is_array check 
    if(is_array($terms_ids)) wp_update_term_count_now($terms_ids, 'solutions_sectors');
  }
}

That did the trick.这就是诀窍。 After running it is important to comment out the init action call.运行后,注释掉 init 操作调用很重要。

If get_terms doesnt works for some strange reason with custom taxonomy not showing registered try using WP_Term_Query :如果get_terms由于某种奇怪的原因get_terms ,自定义分类法未显示已注册,请尝试使用WP_Term_Query

$term_query = new WP_Term_Query( array( 
    'taxonomy' => 'regions', // <-- Custom Taxonomy name..
    'orderby'                => 'name',
    'order'                  => 'ASC',
    'child_of'               => 0,
    'parent' => 0,
    'fields'                 => 'all',
    'hide_empty'             => false,
    ) );


// Show Array info
echo "<pre>";
print_r($term_query->terms);
echo "</pre>";


//Render html
if ( ! empty( $term_query->terms ) ) {
foreach ( $term_query ->terms as $term ) {
echo $term->name .", ";
echo $term->term_id .", ";
echo $term->slug .", ";
echo "<br>";
}
} else {
echo '‘No term found.’';
}

Get all args from here: https://developer.wordpress.org/reference/classes/WP_Term_Query/__construct/从这里获取所有参数: https : //developer.wordpress.org/reference/classes/WP_Term_Query/__construct/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM