Php WordPress统计不同自定义帖子类型的用户帖子
我一直在寻找一种方法来计算用户创建的自定义帖子的数量,并且能够使用以下代码段完成此操作:Php WordPress统计不同自定义帖子类型的用户帖子,php,sql,wordpress,Php,Sql,Wordpress,我一直在寻找一种方法来计算用户创建的自定义帖子的数量,并且能够使用以下代码段完成此操作: <?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 $w
<?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);
} ?>
将第二个post\u类型添加到查询中:
$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'";
将第二个post_类型添加到查询:
$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'";
尝试下面给出的自定义函数
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 count_user_posts_by_type($userid); ?>
<?php echo my_count_posts_by_user($userid , array('posttype1' , 'posttype2')); ?>
请查看下面给出的url
谢谢尝试下面提供的自定义功能
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 count_user_posts_by_type($userid); ?>
<?php echo my_count_posts_by_user($userid , array('posttype1' , 'posttype2')); ?>
请查看下面给出的url
谢谢
您可以在$args中传递任何支持query\u posts的参数
您可以在$args中传递任何参数,这些参数支持查询文章简短、简单且有效。我最终使用了这个基于SQL的解决方案,而不是修改我的自定义函数,尽管两者都可以完成这项工作。简短、简单、有效。我最终使用了这个基于SQL的解决方案,而不是修改我的自定义函数,尽管两者都可以完成这项工作。