【问题标题】:Use method to grab metadata and return as array使用方法获取元数据并作为数组返回
【发布时间】:2020-02-13 15:09:39
【问题描述】:

我有以下方法,我想从数据库中检索一些详细信息并将它们返回到我的方法中:

这是我拥有的get_method

public function get_authors()
    {

        // @todo: Return an array of profile objects
        // @todo: This should turn an array of profiles

        $profiles = get_post_meta(get_the_ID(),'author', true);
        var_dump($profiles);

        return $profiles;
}

这是$profiles var_dump 的内容:

在我的数据库中链接为“作者”:

在我的wp_posts 中,这两个 ID 与帖子相关联 - 我将如何在我的方法中提取这些帖子?

【问题讨论】:

  • 所以你基本上是想返回那些作者在“个人资料”中的帖子?
  • @EbrahimMohammed,正确,我正在输出一个只返回这些配置文件名称的 foreach - 每个“作者”ID 都链接到一个 wp_post
  • 您希望它采用以下格式:数组( ['834'] => posts_array,['839'] => posts_array )还是类似的格式?关键是作者ID?
  • 我想使用作者 ID 并获取特定于这些 ID @EbrahimMohammed 的帖子的输出。所以假设我们有来自作者的 ID 834,这是 wp_posts 内的帖子 -我想将两个完整的帖子抓取到一个数组中

标签: php wordpress class


【解决方案1】:
public function get_authors()
    {

        // @todo: Return an array of profile objects
        // @todo: This should turn an array of profiles

        $profiles = get_post_meta(get_the_ID(),'author', true);

        $final_array = array(); 

        foreach($profiles as $profile){
           $final_array[$profile] = get_posts([
              'author'    =>  $profile, 
              'orderby'       =>  'date',
              'order'         =>  'DESC' 
           ]);

        }
        return $final_array;
}

在这里,这将简单地遍历作者,并获取每个作者的帖子。

【讨论】:

  • 非常感谢@Ebrahim - 我尝试了这种方法,当我 var_dump $final_array 时,我得到空数组结果。
  • 啊,两位作者的结果都是空的?
  • 是的,我已经更新了上图 - 给我 30 秒。
  • 知道了,给我一点时间调试一下!
  • 这是我的荣幸!哇,这很奇怪,它应该可以工作:O 你介意去数据库和帖子表并查询类似的内容: SELECT * FROM posts WHERE post_author='834' 但是用你的帖子替换'posts'不管它是表!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-19
  • 1970-01-01
  • 2013-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-18
相关资源
最近更新 更多