How can I get all custom posts (post_type=family_guy) with their ID and all of their tags in multidimensional array?

Here is exactly how I would like my array to look like:

$array = array("1" =>   Array(
      "2" =>   Array(
      "3" =>   Array(
     "4" =>   Array(

In this array, keys will be custom posts IDs, and values will be all tags of that custom post.

Thanks in advance.

  • 1
    I'm wondering why you tagged the question with wordpress.com, is your blog hosted there? – birgire Oct 4 '13 at 10:58

This does a query for all posts of type family guy, tosses them into the $fg_obj then loops through each post, grabs its tags, and creates a flat array of just the tag name. Then that array gets put into a new array with the post ID. This seems like a strange way of getting this info (because I don't know the final purpose of this array), but it should be the format you've requested.

$args = array( 'post_type' => 'family_guy', 'posts_per_page' => -1 ); 
$fg_obj = get_posts($args);
$posts_with_tags = array();
foreach($fg_obj as $fg) {
  $tags = get_the_tags($fg->ID);
  $tag_arr = array();
  foreach($tags as $tag) $tag_arr[] = $tag->name;
  $posts_with_tags[$fg->ID] = $tag_arr;
  • That was what I was looking for, but I shortened the code with wp_get_post_tags function. Thank u for solution. – Duka Nikolau Oct 8 '13 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.