duangongqiong6958 2016-09-30 11:35
浏览 51
已采纳

Wordpress:即使条件有对象,get_terms()也不会返回任何内容

Normal behaviour of get_terms is not to return terms if there are no posts assigned to. But this is not the case, I can see terms assigned in admin and also checked the database and all seems fine. 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. 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)

  • 写回答

1条回答 默认 最新

  • donglu7286 2016-09-30 12:42
    关注

    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.

    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.

    The solutions here is a one-shot call to 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.

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏