【问题标题】:WordPress count user posts of different custom post typesWordPress 统计不同自定义帖子类型的用户帖子
【发布时间】:2012-12-28 09:55:49
【问题描述】:

我一直在寻找一种方法来计算用户创建的自定义帖子的数量,并且能够使用这个 sn-p 来做到这一点:

<?php

    $userid = get_current_user_id();

    function count_user_posts_by_type($userid, $post_type = 'foo_type', $post_status = 'publish') {

    global $wpdb; 
    $query = "SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = $userid AND post_type = '$post_type' AND post_status = '$post_status'"; 
    $count = $wpdb->get_var($query); 
    return apply_filters('get_usernumposts', $count, $userid);

} ?>

然后将结果回显:

<?php echo count_user_posts_by_type($userid); ?>

我的问题:上面的代码只输出自定义帖子类型“foo_type”的计数。如果我有两个自定义帖子类型 - “foo_type”和“bar_type” - 我如何更改此代码以便返回它们的计数,而不仅仅是“foo_type”的计数?

【问题讨论】:

    标签: php sql wordpress


    【解决方案1】:

    将第二个 post_type 添加到查询中:

    $query = "SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = $userid AND (post_type = '$post_type' OR post_type='$post_type_2') AND post_status = '$post_status'"; 
    

    【讨论】:

    • 简短、简单、有效。我最终使用了这个基于 SQL 的解决方案,而不是更改我的自定义函数,尽管两者都能胜任。
    【解决方案2】:

    试试下面给出的自定义函数

    function my_count_posts_by_user($post_author=null,$post_type=array(),$post_status=array()) {
        global $wpdb;
    
        if(empty($post_author))
            return 0;
    
        $post_status = (array) $post_status;
        $post_type = (array) $post_type;
    
        $sql = $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = %d AND ", $post_author );
    
        //Post status
        if(!empty($post_status)){
            $argtype = array_fill(0, count($post_status), '%s');
            $where = "(post_status=".implode( " OR post_status=", $argtype).') AND ';
            $sql .= $wpdb->prepare($where,$post_status);
        }
    
        //Post type
        if(!empty($post_type)){
            $argtype = array_fill(0, count($post_type), '%s');
            $where = "(post_type=".implode( " OR post_type=", $argtype).') AND ';
            $sql .= $wpdb->prepare($where,$post_type);
        }
    
        $sql .='1=1';
        $count = $wpdb->get_var($sql);
        return $count;
    } 
    

    然后将结果回显:

    <?php echo my_count_posts_by_user($userid , array('posttype1' , 'posttype2')); ?>
    

    请查看下面给出的网址..

    https://wordpress.stackexchange.com/questions/43549/count-user-posts-by-user-id-post-type-and-post-status

    谢谢

    【讨论】:

      【解决方案3】:
      $args = array(
          'post_type'=> 'page',
          'author'    => '1',
          'post_staus'=> 'publish',
          'posts_per_page' => -1
      );
      echo custom_count_post_by_author($args);
      function custom_count_post_by_author($args){
          query_posts( $args );
          $count = 0;
          if ( have_posts() ) :
               while ( have_posts() ) : the_post();
                  $count++;
               endwhile; 
          endif;
          wp_reset_query();
          return $count;
      }
      

      您可以在 $args 中传递任何支持 query_posts

      的参数

      【讨论】:

        猜你喜欢
        • 2016-12-29
        • 1970-01-01
        • 2016-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-29
        • 1970-01-01
        • 2015-12-22
        相关资源
        最近更新 更多