是否从自定义分类中的帖子计数中删除“垃圾”帖子?

时间:2011-08-22 作者:Dan Lee

我发现了一个计算自定义分类术语中帖子数量的函数。问题是,当我“丢弃”一篇文章时,它仍然会出现在计数中。有没有办法解决这个问题?

放置在函数中。php:

//Job count

function wt_get_category_count($input = \'\') {
global $wpdb;
if($input == \'\')
{
$category = get_the_category();
return $category[0]->category_count;
}
elseif(is_numeric($input))
{
$SQL = "SELECT $wpdb->term_taxonomy.count FROM $wpdb->terms, $wpdb->term_taxonomy WHERE $wpdb->terms.term_id=$wpdb->term_taxonomy.term_id AND $wpdb->term_taxonomy.term_id=$input";
return $wpdb->get_var($SQL);
}
else
{
$SQL = "SELECT $wpdb->term_taxonomy.count FROM $wpdb->terms, $wpdb->term_taxonomy WHERE $wpdb->terms.term_id=$wpdb->term_taxonomy.term_id AND $wpdb->terms.slug=\'$input\'";
return $wpdb->get_var($SQL);
}
}
放置在模板文件中:(按id定位的术语)

<?php echo wt_get_category_count(\'26\'); ?>

2 个回复
SO网友:Adam

是否尝试使用post\\U状态添加到查询的对象?

e、 g。

$SQL = "SELECT $wpdb->term_taxonomy.count FROM $wpdb->terms, $wpdb->term_taxonomy WHERE $wpdb->terms.term_id=$wpdb->term_taxonomy.term_id AND $wpdb->term_taxonomy.term_id=$input AND post_status = \'publish\'";
return $wpdb->get_var($SQL);

SO网友:Chip Bennett

这是a known, reported WordPress bug.

有一些变通方法,例如this one:

add_action( \'edited_term_taxonomy\',\'wpse26548_edited_term_taxonomy\', 10, 2 );
function wpse26548_edited_term_taxonomy($term,$taxonomy) {
  global $wpdb,$post;
  //in quick edit mode, $post is an array()
  //in full edit mode $post is an object
  if ( is_array( $post ))
    $posttype=$post[\'post_type\'];
  else
    $posttype=$post->post_type;
  if ($posttype) {
    $DB_prefix=$wpdb->get_blog_prefix(BLOG_ID_CURRENT_SITE);
    $sql = "UPDATE ".$DB_prefix."term_taxonomy tt
          SET count =
          (SELECT count(p.ID) FROM  ".$DB_prefix."term_relationships tr
          LEFT JOIN ".$DB_prefix."posts p
          ON (p.ID = tr.object_id AND p.post_type = \'".$posttype."\' AND p.post_status = \'publish\')
          WHERE tr.term_taxonomy_id = tt.term_taxonomy_id)
          WHERE tt.taxonomy = \'".$taxonomy->name."\'
      ";
    $wpdb->query($sql);
  }
}
您可能还需要将以下内容添加到register_taxonomy() 参数数组:

\'update_count_callback\' => \'_update_post_term_count\'
请注意,这些都是完全未经测试的。

(另请参见:this related WPSE question)

结束

相关推荐