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(
"Peter1",
"Lois1",
"Megan1"
),
"2" => Array(
"Peter2",
"Lois2",
"Megan2"
),
"3" => Array(
"Peter3",
"Lois3",
"Megan3"
),
"4" => Array(
"Peter4",
"Lois4",
"Megan4"
)
);
In this array, keys will be custom posts IDs, and values will be all tags of that custom post.
Thanks in advance.
Something along these lines should help, in functions.php
function cpt_tag_list() {
$types = get_post_types(array(
'_builtin' => false // This returns only custom post types
'public' => true // Not necessarily right, but consider this, you may need to filter out CPTs made by plugins.
));
$op = array();
$i = 0;
foreach ($types as $cpt) {
$i++;
$posts_in_cpt = new WP_Query("posts_per_page=-1&post_type={$cpt}");
$tags_used = array();
foreach ($posts_in_cpt as $post) {
$tags_used[] = get_tags($post->ID);
}
$op[(string) $i] = array_unique($tags_used);
}
return $op
}
And then invoke in your site by calling cpt_tag_list();
Apologies if I've made any errors in this, I've not had chance to test it, but hopefully the gist of it is there for you.