首页 > 解决方案 > Wordpress:通过 meta_key 回显帖子作者的帖子总数?

问题描述

好的,所以我知道如何回显帖子的 meta_key 值:

<?php 
$meta_print_value=get_post_meta(get_the_ID(),'_heart_this',true);
echo($meta_print_value);
?>

我也知道如何用这个特定的 meta_key 回显所有帖子:

<?php  $query = new WP_Query( array( 'meta_key' => '_heart_this' ) ) 
echo $query->found_posts; ?>

我需要的是帖子作者的这个 meta_key 的帖子总数。

我知道我需要get_the_author()在上面的代码中添加某处以仅显示帖子作者的帖子总数,但现在挣扎了一段时间。

任何帮助,将不胜感激。

标签: phpwordpress

解决方案


在 WP_Query() 中使用作者参数。您可以使用作者 ID/姓名/多位作者。对于您的情况,查询应如下所示:

<?php
$query = new WP_Query(
    array(
        'author' => get_the_author_meta( 'ID' );
        'meta_key' => '_heart_this',
    ),
 );
echo $query->found_posts; ?>

推荐阅读