Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
function get_posts_by_category( $category_name )
{
$args = array(
'posts_per_page' => 30,
'offset' => 0,
'category' => $category_name,
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'represent_map',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
return get_posts($args);
}
$terms = get_categories(array(
'type' => 'represent_map',
'taxonomy' => 'represent_map_type')
);
$categories = array();
if (!empty($terms)) {
foreach ($terms as $t) {
if (0 == $t->parent) {
$categories[$t->term_id] = $t;
// Obtendo os posts
$categories[$t->term_id]->posts = get_posts_by_category($t->slug);
unset($terms[$t->term_id]);
}
}
}
foreach($terms as $cat) {
if ( !empty($cat->name) && !empty($cat->parent) ) {
//Obtendo os posts
$cat->posts = get_posts_by_category($t->slug);
$categories[$cat->parent]->children[] = $cat;
}
}
print_r($categories);
//Saída
/*
Array
(
[18] => stdClass Object
(
[term_id] => 18
[name] => Multiplataforma
[slug] => multiplataforma
[term_group] => 0
[term_taxonomy_id] => 18
[taxonomy] => represent_map_type
[description] =>
[parent] => 0
[count] => 1
[cat_ID] => 18
[category_count] => 1
[category_description] =>
[cat_name] => Multiplataforma
[category_nicename] => multiplataforma
[category_parent] => 0
[posts] => Array
(
[0] => WP_Post Object
(
[ID] => 159
[post_author] => 1
[post_date] => 2014-02-08 20:03:17
[post_date_gmt] => 2014-02-08 20:03:17
[post_content] =>
[post_title] => Casa doCliente
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => casa-do-cliente
[to_ping] =>
[pinged] =>
[post_modified] => 2014-02-08 22:44:13
[post_modified_gmt] => 2014-02-08 22:44:13
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://localhost:8080/desenvolvimento/outdoor-midia/?post_type=represent_map&#038;p=159
[menu_order] => 0
[post_type] => represent_map
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[1] => WP_Post Object
(
[ID] => 158
[post_author] => 1
[post_date] => 2014-02-08 18:20:27
[post_date_gmt] => 2014-02-08 18:20:27
[post_content] =>
[post_title] => teste
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => teste-2
[to_ping] =>
[pinged] =>
[post_modified] => 2014-02-08 20:24:22
[post_modified_gmt] => 2014-02-08 20:24:22
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://localhost:8080/desenvolvimento/outdoor-midia/?post_type=represent_map&#038;p=158
[menu_order] => 0
[post_type] => represent_map
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[2] => WP_Post Object
(
[ID] => 103
[post_author] => 1
[post_date] => 2014-01-24 04:39:35
[post_date_gmt] => 2014-01-24 04:39:35
[post_content] =>
[post_title] => Lonado novo
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => lonado-novo
[to_ping] =>
[pinged] =>
[post_modified] => 2014-02-08 20:51:33
[post_modified_gmt] => 2014-02-08 20:51:33
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://localhost:8080/desenvolvimento/outdoor-midia/?post_type=represent_map&#038;p=103
[menu_order] => 0
[post_type] => represent_map
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[3] => WP_Post Object
(
[ID] => 87
[post_author] => 1
[post_date] => 2014-01-24 02:39:21
[post_date_gmt] => 2014-01-24 02:39:21
[post_content] => Tamanho: 15x12
Digital: Sim
&nbsp;
[post_title] => Cartaz 9x3 ecovia
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => cartaz-9x3-ecovia
[to_ping] =>
[pinged] =>
[post_modified] => 2014-01-24 02:39:21
[post_modified_gmt] => 2014-01-24 02:39:21
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://localhost:8080/desenvolvimento/outdoor-midia/?post_type=represent_map&#038;p=87
[menu_order] => 0
[post_type] => represent_map
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
)
)
[9] => stdClass Object
(
[term_id] => 9
[name] => Outdoor Tradicional
[slug] => outdoor-tradicional
[term_group] => 0
[term_taxonomy_id] => 9
[taxonomy] => represent_map_type
[description] =>
[parent] => 0
[count] => 1
[cat_ID] => 9
[category_count] => 1
[category_description] =>
[cat_name] => Outdoor Tradicional
[category_nicename] => outdoor-tradicional
[category_parent] => 0
[posts] => Array
(
[0] => WP_Post Object
(
[ID] => 159
[post_author] => 1
[post_date] => 2014-02-08 20:03:17
[post_date_gmt] => 2014-02-08 20:03:17
[post_content] =>
[post_title] => Casa doCliente
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => casa-do-cliente
[to_ping] =>
[pinged] =>
[post_modified] => 2014-02-08 22:44:13
[post_modified_gmt] => 2014-02-08 22:44:13
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://localhost:8080/desenvolvimento/outdoor-midia/?post_type=represent_map&#038;p=159
[menu_order] => 0
[post_type] => represent_map
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[1] => WP_Post Object
(
[ID] => 158
[post_author] => 1
[post_date] => 2014-02-08 18:20:27
[post_date_gmt] => 2014-02-08 18:20:27
[post_content] =>
[post_title] => teste
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => teste-2
[to_ping] =>
[pinged] =>
[post_modified] => 2014-02-08 20:24:22
[post_modified_gmt] => 2014-02-08 20:24:22
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://localhost:8080/desenvolvimento/outdoor-midia/?post_type=represent_map&#038;p=158
[menu_order] => 0
[post_type] => represent_map
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[2] => WP_Post Object
(
[ID] => 103
[post_author] => 1
[post_date] => 2014-01-24 04:39:35
[post_date_gmt] => 2014-01-24 04:39:35
[post_content] =>
[post_title] => Lonado novo
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => lonado-novo
[to_ping] =>
[pinged] =>
[post_modified] => 2014-02-08 20:51:33
[post_modified_gmt] => 2014-02-08 20:51:33
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://localhost:8080/desenvolvimento/outdoor-midia/?post_type=represent_map&#038;p=103
[menu_order] => 0
[post_type] => represent_map
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[3] => WP_Post Object
(
[ID] => 87
[post_author] => 1
[post_date] => 2014-01-24 02:39:21
[post_date_gmt] => 2014-01-24 02:39:21
[post_content] => Tamanho: 15x12
Digital: Sim
&nbsp;
[post_title] => Cartaz 9x3 ecovia
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => cartaz-9x3-ecovia
[to_ping] =>
[pinged] =>
[post_modified] => 2014-01-24 02:39:21
[post_modified_gmt] => 2014-01-24 02:39:21
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://localhost:8080/desenvolvimento/outdoor-midia/?post_type=represent_map&#038;p=87
[menu_order] => 0
[post_type] => represent_map
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
)
[children] => Array
(
[0] => stdClass Object
(
[term_id] => 5
[name] => Cartaz 9x3
[slug] => cartaz-9x3
[term_group] => 0
[term_taxonomy_id] => 5
[taxonomy] => represent_map_type
[description] =>
[parent] => 9
[count] => 1
[cat_ID] => 5
[category_count] => 1
[category_description] =>
[cat_name] => Cartaz 9x3
[category_nicename] => cartaz-9x3
[category_parent] => 9
[posts] => Array
(
[0] => WP_Post Object
(
[ID] => 159
[post_author] => 1
[post_date] => 2014-02-08 20:03:17
[post_date_gmt] => 2014-02-08 20:03:17
[post_content] =>
[post_title] => Casa doCliente
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => casa-do-cliente
[to_ping] =>
[pinged] =>
[post_modified] => 2014-02-08 22:44:13
[post_modified_gmt] => 2014-02-08 22:44:13
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://localhost:8080/desenvolvimento/outdoor-midia/?post_type=represent_map&#038;p=159
[menu_order] => 0
[post_type] => represent_map
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[1] => WP_Post Object
(
[ID] => 158
[post_author] => 1
[post_date] => 2014-02-08 18:20:27
[post_date_gmt] => 2014-02-08 18:20:27
[post_content] =>
[post_title] => teste
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => teste-2
[to_ping] =>
[pinged] =>
[post_modified] => 2014-02-08 20:24:22
[post_modified_gmt] => 2014-02-08 20:24:22
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://localhost:8080/desenvolvimento/outdoor-midia/?post_type=represent_map&#038;p=158
[menu_order] => 0
[post_type] => represent_map
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[2] => WP_Post Object
(
[ID] => 103
[post_author] => 1
[post_date] => 2014-01-24 04:39:35
[post_date_gmt] => 2014-01-24 04:39:35
[post_content] =>
[post_title] => Lonado novo
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => lonado-novo
[to_ping] =>
[pinged] =>
[post_modified] => 2014-02-08 20:51:33
[post_modified_gmt] => 2014-02-08 20:51:33
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://localhost:8080/desenvolvimento/outdoor-midia/?post_type=represent_map&#038;p=103
[menu_order] => 0
[post_type] => represent_map
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[3] => WP_Post Object
(
[ID] => 87
[post_author] => 1
[post_date] => 2014-01-24 02:39:21
[post_date_gmt] => 2014-01-24 02:39:21
[post_content] => Tamanho: 15x12
Digital: Sim
&nbsp;
[post_title] => Cartaz 9x3 ecovia
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => cartaz-9x3-ecovia
[to_ping] =>
[pinged] =>
[post_modified] => 2014-01-24 02:39:21
[post_modified_gmt] => 2014-01-24 02:39:21
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://localhost:8080/desenvolvimento/outdoor-midia/?post_type=represent_map&#038;p=87
[menu_order] => 0
[post_type] => represent_map
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
)
)
[1] => stdClass Object
(
[term_id] => 7
[name] => Lonado 9x3
[slug] => lonado-9x3
[term_group] => 0
[term_taxonomy_id] => 7
[taxonomy] => represent_map_type
[description] =>
[parent] => 9
[count] => 1
[cat_ID] => 7
[category_count] => 1
[category_description] =>
[cat_name] => Lonado 9x3
[category_nicename] => lonado-9x3
[category_parent] => 9
[posts] => Array
(
[0] => WP_Post Object
(
[ID] => 159
[post_author] => 1
[post_date] => 2014-02-08 20:03:17
[post_date_gmt] => 2014-02-08 20:03:17
[post_content] =>
[post_title] => Casa doCliente
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => casa-do-cliente
[to_ping] =>
[pinged] =>
[post_modified] => 2014-02-08 22:44:13
[post_modified_gmt] => 2014-02-08 22:44:13
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://localhost:8080/desenvolvimento/outdoor-midia/?post_type=represent_map&#038;p=159
[menu_order] => 0
[post_type] => represent_map
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[1] => WP_Post Object
(
[ID] => 158
[post_author] => 1
[post_date] => 2014-02-08 18:20:27
[post_date_gmt] => 2014-02-08 18:20:27
[post_content] =>
[post_title] => teste
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => teste-2
[to_ping] =>
[pinged] =>
[post_modified] => 2014-02-08 20:24:22
[post_modified_gmt] => 2014-02-08 20:24:22
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://localhost:8080/desenvolvimento/outdoor-midia/?post_type=represent_map&#038;p=158
[menu_order] => 0
[post_type] => represent_map
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[2] => WP_Post Object
(
[ID] => 103
[post_author] => 1
[post_date] => 2014-01-24 04:39:35
[post_date_gmt] => 2014-01-24 04:39:35
[post_content] =>
[post_title] => Lonado novo
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => lonado-novo
[to_ping] =>
[pinged] =>
[post_modified] => 2014-02-08 20:51:33
[post_modified_gmt] => 2014-02-08 20:51:33
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://localhost:8080/desenvolvimento/outdoor-midia/?post_type=represent_map&#038;p=103
[menu_order] => 0
[post_type] => represent_map
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[3] => WP_Post Object
(
[ID] => 87
[post_author] => 1
[post_date] => 2014-01-24 02:39:21
[post_date_gmt] => 2014-01-24 02:39:21
[post_content] => Tamanho: 15x12
Digital: Sim
&nbsp;
[post_title] => Cartaz 9x3 ecovia
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => cartaz-9x3-ecovia
[to_ping] =>
[pinged] =>
[post_modified] => 2014-01-24 02:39:21
[post_modified_gmt] => 2014-01-24 02:39:21
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://localhost:8080/desenvolvimento/outdoor-midia/?post_type=represent_map&#038;p=87
[menu_order] => 0
[post_type] => represent_map
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
)
)
)
)
)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment