WordPress 在不使用 get_posts() 的情况下获取帖子数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2134022/
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
WordPress get count of posts without using get_posts()?
提问by Scott B
Need a function call that is designed solely to get the count of posts matching a criteria. I believe the get_posts() function is too expensive for this operation. I'm merely trying to decide whether or not to show a "View More Posts" link when there are a predefined number of posts to display...
需要一个专门设计用于获取符合条件的帖子计数的函数调用。我相信 get_posts() 函数对于这个操作来说太昂贵了。我只是想决定当有预定义数量的帖子要显示时是否显示“查看更多帖子”链接......
For example, the default number of post links to display is 3. I only want to show a "View More Posts" link if the total number of posts exceeds 3.
例如,要显示的帖子链接的默认数量是 3。如果帖子总数超过 3,我只想显示“查看更多帖子”链接。
Here's my code...
这是我的代码...
$cat1=get_cat_ID('test');
$cat2=get_cat_ID('test2');
$myposts = get_posts(array('cat' => "$cat1,-$cat2",'showposts' => 3));
$myposts2 = get_posts(array('cat' => "$cat1,-$cat2"));
$mypostcount = count($myposts2);
foreach($myposts as $idx=>$post)
?>
<li><a>Post Info Goes here</a></li>
<?php
if ($mypostscount > 3){ ?>Show View All Link<?php ?>
回答by David
You're question wasn't completely clear, so here's two methods.
你的问题不是很清楚,所以这里有两种方法。
First, if the user is viewing a category page and you want to display this information, you can simply use the following:
首先,如果用户正在查看类别页面并且您想要显示此信息,您可以简单地使用以下内容:
$myCount = $wp_query->found_posts;
That will return the number of posts found for the last query.
这将返回为上次查询找到的帖子数。
If you want to count the number of posts for each category, say for like a homepage I would go about it simply through PHP/MySQL. Here's an example:
如果你想计算每个类别的帖子数量,比如主页,我会简单地通过 PHP/MySQL 进行处理。下面是一个例子:
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 = 'category' AND cat_terms.term_id = '13'
I just tested it and it worked properly. Once you get the return from the query just simply grab the row and then do as follows:
我刚刚测试了它,它工作正常。一旦您从查询中获得返回,只需简单地获取该行,然后执行以下操作:
echo $row['post_count'];
or whatever you like with the data. All you need to do to change categories is simply change the last WHERE clause's term_id
或者任何你喜欢的数据。更改类别所需要做的只是更改最后一个 WHERE 子句的 term_id
cat_terms.term_id = '13'
Change 13 to the cat you would like to count.
将 13 更改为您要数的猫。
If you would instead like to do it by category name you could change the last part from
如果您想按类别名称进行操作,则可以将最后一部分从
cat_terms.term_id = '13'
to
到
cat_terms.slug IN ('cookies', 'uncategorized') or cat_terms.slug IN ('cookies')
The first one will select from multiple categories, the second from only one. Hope this helps,
第一个将从多个类别中进行选择,第二个仅从一个类别中进行选择。希望这可以帮助,
回答by Gipetto
You can do a custom WP_Query in which you use a filter on posts_fields
to change what is returned by the query. So use WP_Query as you normally would to get the specific list of posts that you're after and change the fields that are pulled to get the COUNT(wp_posts.ID)
.
您可以执行自定义 WP_Query,在其中使用过滤器posts_fields
更改查询返回的内容。因此,像往常一样使用 WP_Query 来获取您所关注的特定帖子列表,并更改为获取COUNT(wp_posts.ID)
.
Also make sure you change posts_per_page to -1
so that you get all posts.
还要确保将 posts_per_page 更改为,-1
以便获得所有帖子。
But, also, as noted above by David, the query objects will have the found_posts member that will tell you how many total posts matched the criteria. So your solution may be to use full WP_Query objects instead of using get_posts to get your list.
但是,正如 David 上面提到的,查询对象将具有 found_posts 成员,该成员将告诉您与条件匹配的帖子总数。因此,您的解决方案可能是使用完整的 WP_Query 对象而不是使用 get_posts 来获取您的列表。
回答by davidosomething
As of wordpress 2.5 a function exists for this: http://codex.wordpress.org/Function_Reference/wp_count_posts
从 wordpress 2.5 开始,存在一个函数:http: //codex.wordpress.org/Function_Reference/wp_count_posts
If you don't trust it, use a $wpdb query or PHP/MySQL without the wordpress wrapper
如果您不信任它,请使用 $wpdb 查询或没有 wordpress 包装器的 PHP/MySQL