我将如何使用博客/站点id来计算id指定站点上的帖子,就像wp_count_posts()对当前站点所做的那样?
我试图为AJAX访问的端点创建一个函数,用户可以在其中从select元素中选择一个站点,以查看其他站点上有多少帖子。
我真的想避免直接查询数据库。
发布于 2018-02-17 20:07:36
为了像在单个站点上一样使用wp_count_posts(),在调用wp_count_posts()之前,将博客id传递到switch_to_blog()函数中。
示例:
function wpse_get_post_count( $blog_id ){
switch_to_blog( $blog_id );
$post_count = wp_count_posts();
restore_current_blog();
return $post_count;
}确保restore_current_blog()是在每个switch_to_blog()之后使用的,否则您将面临文件说明一节和这个WordPress堆栈交换答案中提到的困难。
https://wordpress.stackexchange.com/questions/294401
复制相似问题