WordPress获取不同用户类型文章和自定义字段下文章数量

通常在 wordpress 主题开发中,集成了个人中心功能,需要获取文章、视频、下载、专题和商城的文章数量,这个是比较简单,使用 wp_count_posts()函数即可。如果要获取不同用户、不同自定义字段下的不同文章类型的文章数量怎么办,同样很好解决,使用 get_posts()函数。
一、代码

  1.     /*不同用户,不同文章类型,不同字段下的文章数量*/
  2.     function salong_author_post_field_count($post_type,$user_id,$field) {
  3.         global $post;
  4.         $post_args = get_posts( array(
  5.             'posts_per_page' => -1,
  6.             'post_type'      => $post_type,
  7.             'post_status'    => 'publish',
  8.             'author'         => $user_id
  9.         ) );
  10.         $counter = 0;
  11.         foreach ( $post_args as $post ){
  12.             $views = absint( get_post_meta( $post->ID, $field, true ) );
  13.             $counter += $views;
  14.         }
  15.         return $counter;
  16.     }

二、调用
调用很方便,比如要调用 ID 为 1 的用户,视频(video)类型文章下的有浏览量(views)的文章数量:

  1. echo salong_author_post_field_count('video','1','views');

服务范围1、专业提供WordPress主题、插件汉化、优化、PHP环境配置等服务请详询在线客服
2、本站承接 WordPress、DedeCMS、ThinkPHP 等系统建站、仿站、开发、定制等服务
3、英文模板(主题)安装费用为120元/次,汉化主题首次免费安装(二次安装30元/次)
售后时间周一至周五(法定节假日除外) 10:00-22:00
免责声明本站所提供的模板(主题/插件)等资源仅供学习交流,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担,有部分资源为网上收集或仿制而来,若模板侵犯了您的合法权益,请来信通知我们(Email: [email protected]),我们会及时删除,给您带来的不便,我们深表歉意!

.demo tbody td a strong{color: #666;}.button_nowbuy,.button_demo {margin: 0em auto;border-style: none;border-width: 0px;padding: 0px;}#qrcode_en,#qrcode_cn{width: 130px;left: 10%;bottom: 250%;}a.button_demo:hover + #qrcode_en{visibility:visible;opacity: 1;bottom: 180%;-webkit-transition: all .4s ease-in-out;-o-transition: all .4s ease-in-out;transition: all .4s ease-in-out;}a.button_demo:hover + #qrcode_cn{visibility:visible;opacity: 1;bottom: 180%;-webkit-transition: all .4s ease-in-out;-o-transition: all .4s ease-in-out;transition: all .4s ease-in-out;}.table.demo{display: none;}

© 版权声明
THE END
喜欢就支持以下吧
点赞0赞赏 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容