Home » Php » php – Use method to grab metadata and return as array

php – Use method to grab metadata and return as array

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have the below method that I want to retrieve some details from the database and return them inside my method:

Here is the get_method that I have:

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;
}

Here is what $profiles var_dump’s:
enter image description here

That is linked in my database as such ‘author’:
enter image description here

Inside my wp_posts, those two ID’s are linked to posts – How would I go about extracting those posts inside my method?

enter image description here

How to&Answers:
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;
}

Here you go, this would simply loop over the author, and get the posts of each author.