WordPress get count of posts without using get_pos

2020-07-25 02:36发布

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...

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.

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 ?>

标签: wordpress
3条回答
不美不萌又怎样
2楼-- · 2020-07-25 03:09

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:

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

cat_terms.term_id = '13'

Change 13 to the cat you would like to count.

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,

查看更多
劫难
3楼-- · 2020-07-25 03:16

As of wordpress 2.5 a function exists for this: 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

查看更多
走好不送
4楼-- · 2020-07-25 03:28

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).

Also make sure you change posts_per_page to -1 so that you get all posts.

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.

查看更多
登录 后发表回答