在自定义分类中对帖子进行计数

时间:2012-12-15 作者:user1706680

有没有办法从自定义分类法中统计所有已发布的帖子?

环顾四周,我发现this snippet 但我没能成功…

global $wpdb;
$query = "
        SELECT COUNT( DISTINCT cat_posts.ID ) AS post_count
        FROM wp_term_taxonomy AS cat_term_taxonomy INNER JOIN wp_terms AS cat_terms ON
        cat_term_taxonomy.term_id = cat_terms.term_id
        INNER JOIN wp_term_relationships AS cat_term_relationships 
        ON cat_term_taxonomy.term_taxonomy_id = cat_term_relationships.term_taxonomy_id
        INNER JOIN wp_posts AS cat_posts 
        ON cat_term_relationships.object_id = cat_posts.ID
        WHERE cat_posts.post_status = \'publish\' 
        AND cat_posts.post_type = \'post\' 
        AND cat_term_taxonomy.taxonomy = \'YOUR-CUSTOM-TAXONOMY\' 
        AND cat_terms.slug IN (\'TERM-SLUG-1, TERM-SLUG-2\')
    ";
return $wpdb->get_var($query);

9 个回复
最合适的回答,由SO网友:Upeksha 整理而成

使用的实例WP_Query 查询数据库。http://codex.wordpress.org/Class_Reference/WP_Query

要查询数据库以获取自定义分类,请使用,

$query = new WP_Query( array( \'people\' => \'bob\' ) );
有关可用选项的更多详细信息,请参阅:Taxonomy Parametershttp://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

使用检索已发布的帖子

\'post_status\' => \'publish\'
使用found_posts 检索职位数量

$count = $query->found_posts;

SO网友:Manchumahara
function wp_get_productcat_postcount($id) {

    //return $count;
    $args = array(
      \'post_type\'     => \'product\', //post type, I used \'product\'
      \'post_status\'   => \'publish\', // just tried to find all published post
      \'posts_per_page\' => -1,  //show all
      \'tax_query\' => array(
        \'relation\' => \'AND\',
        array(
          \'taxonomy\' => \'product_cat\',  //taxonomy name  here, I used \'product_cat\'
          \'field\' => \'id\',
          \'terms\' => array( $id )
        )
      )
    );

    $query = new WP_Query( $args);

    /*
    echo \'<pre>\';

    print_r($query->post_count);
    echo \'</pre>\';
    */

    return (int)$query->post_count;

}
SO网友:squarecandy

我真的很喜欢瓦沙·达奇(VarshaDhadge)简单的回答,但这是一篇文章附带的分类学术语。对于整个站点,使用get_terms 相反,以类似的方式:

$custom_cats = get_terms(
    \'custom_cat\', // your custom taxonomy slug
    array(
        // get all the terms, even empty ones so we can see what items have 0
        \'hide_empty\' => false,
    )
);

foreach ( $custom_cats as $term ) {
    echo $term->slug . \' - \' . $term->count . \'<br>\';
}

SO网友:Varsha Dhadge

下面的代码将获取特定分类法中的文章计数

<?php 
  $terms = get_the_terms( $post->ID , \'your-taxonomy\' );
  foreach ( $terms as $term ) {
    echo $term->count;
  } ?>

SO网友:Benjamin Luoma

可以使用get\\u queryed\\u object()对当前对象执行此操作

$posts = get_queried_object();
echo $posts->count;
否则,您可能会不必要地运行第二个查询,对吗?

SO网友:ashraf mohammed

Wordpress将属性计数添加到每个分类法中,并在插入或更新新帖子时进行更新。因为它不想再次访问数据库并进行计算以获得分类法中的帖子数量。

echo $custom_tax_obj->count;

SO网友:narinder kumar
$args = array(
\'post_type\' => \'product\',
\'post_status\' => \'published\',
\'product_cat\' => $catpage, // $catpage == your category slug name
\'numberposts\' => -1
);
echo $num = count( get_posts( $args ) );
SO网友:cogdog

我在分类法归档模板中面临这种情况(例如。taxonomy-taxname.php) 对于具有自定义帖子类型和关联类别/标记分类的网站。我想在标题中显示使用分类法的项目数。

在我的分类法归档模板中,我需要找到分类法所表示的术语,然后将其传递给一个函数,以获取使用该术语的自定义post类型项目的数量,这是从标记分类法获得的

// the term we need for this taxonomy
$term_obj = get_queried_object(); 

// get the term object
$term = get_term( $term_obj->term_id, \'taxname\' );

// get a count of content with this term
$tax_count = get_tax_count(\'taxname\', $term->slug, \'custom-post-type-name\');

// grammar counts
$plural = ( $tax_count == 1) ? \'\' : \'s\';

echo $tax_count . \' Item\' . $plural . \' Tagged "\' . $term->name . \'"\';
用于计数的函数使用WP\\U查询和tax\\U查询:

function tax_count ( $taxonomy, $term, $post_type ) {
    // find the number of items in custom post type that use the term in a taxonomy

    $args = array(
        \'post_type\' =>  $post_type,
        \'posts_per_page\' => -1,
        \'tax_query\' => array(
            array(
                \'taxonomy\' => $taxonomy,
                \'field\'    => \'slug\',
                \'terms\'    => $term,
            ),
        ),
    );

    $tax_query = new WP_Query( $args );

    return ($tax_query->found_posts);

}

SO网友:CoderSantosh

我根据自己的需求创建了此函数,并与此问题完全匹配。

Get Post Count In or Not In Taxonomy

/**
 * Get Post/Post Type Count in the given Taxonomy
 *
 * @param  $post_type string any post type
 * @param  $taxonomy string any taxonomy
 * @param  $is_exists string NOT EXISTS or EXISTS
 *
 * @return String Number of Post count
 */

function prefix_get_post_count_in_taxonomy($post_type, $taxonomy, $is_exists = \'NOT EXISTS\')
{
    global $wpdb;

    $count = $wpdb->get_var(
        "
select COUNT(*) as post_count  
    FROM $wpdb->posts as A 
WHERE $is_exists (
    SELECT 1
    FROM 
         $wpdb->term_relationships as B
             INNER JOIN $wpdb->term_taxonomy as C
                 ON C.term_taxonomy_id = B.term_taxonomy_id
    WHERE C.taxonomy = \'$taxonomy\'
      AND B.object_id = A.ID
    )                                                    
  AND A.post_type = \'$post_type\'
  "
    );

    return $count;
}


/**
 * Example
 *
 * Post type: download
 * Taxonomy: download_category
 * Is Exist: EXISTS
 */
echo prefix_get_post_count_in_taxonomy(\'download\', \'download_category\', \'EXISTS\');

结束

相关推荐