doumi9661 2010-03-23 23:12
浏览 51
已采纳

WordPress get_post_count?

I'd like to create a function that retrieves the post count for a given query. I don't want to use get_posts obviously as its way to expensive for this purpose. However, that's exactly what I'm having to use in absense of a get_post_count function.

My code is...

global $post;
$cat=get_cat_ID('mymenu');
$catHidden=get_cat_ID('hidden');
$myrecentposts = get_posts(array('post_not_in' => get_option('sticky_posts'), 'cat' => "-$cat,-$catHidden",'showposts' => $NumberOfPostsToShow));
$myrecentposts2 = get_posts(array('post_not_in' => get_option('sticky_posts'), 'cat' => "-$cat,-$catHidden",'showposts' => -1));
$myrecentpostscount = count($myrecentposts2);

Note: get_posts() is a core WP function.

  • 写回答

1条回答 默认 最新

  • dousilie9522 2010-03-24 13:02
    关注

    You could create a new WP_Query object, the found_posts property of which would be your post count.

    $myquery = new WP_Query();
    $myquery->query(array(
        'cat' => "-$cat,-$catHidden",
        'post_not_in' => get_option('sticky_posts'),
        'posts_per_page' => $NumberOfPostsToShow
    ));
    
    $myrecentpostscount = $myquery->found_posts;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器