{"id":31735,"date":"2013-06-28T22:43:06","date_gmt":"2013-06-28T22:43:06","guid":{"rendered":"https:\/\/br.wordpress.org\/support\/topic\/pegar-categoria-do-post\/"},"modified":"2016-08-06T01:22:37","modified_gmt":"2016-08-06T01:22:37","slug":"pegar-categoria-do-post","status":"closed","type":"topic","link":"https:\/\/br.wordpress.org\/support\/topic\/pegar-categoria-do-post\/","title":{"rendered":"Pegar categoria do post"},"content":{"rendered":"<p>Quero saber como posso pegar a categoria de cada post. Fiz esse foreach, mas s\u00f3 a categoria eu n\u00e3o consigo pegar.<\/p>\n<blockquote>\n<p>\tglobal $wpdb;<\/p>\n<p>\t$querystr = &#8220;<br \/>\n\tSELECT DISTINCT $wpdb-&gt;posts.*<br \/>\n\tFROM $wpdb-&gt;posts, $wpdb-&gt;postmeta<br \/>\n\tWHERE $wpdb-&gt;posts.ID = $wpdb-&gt;postmeta.post_id<br \/>\n\tAND $wpdb-&gt;posts.post_status = &#8216;publish&#8217;<br \/>\n\tAND $wpdb-&gt;posts.post_type = &#8216;ad_listing&#8217;<br \/>\n\tAND $wpdb-&gt;posts.post_parent = &#8216;0&#8217;<br \/>\n\tORDER BY $wpdb-&gt;posts.post_date DESC<br \/>\n\t&#8220;;<br \/>\n\t$pageposts = $wpdb-&gt;get_results($querystr, OBJECT);<\/p>\n<\/blockquote>\n<blockquote>\n<p>&lt;?php foreach($pageposts as $post) : setup_postdata($post); $postid = $post-&gt;ID; $user = get_user_by(&#8220;id&#8221;, $post-&gt;post_author); $categories = get_the_category($post-&gt;ID); ?&gt;<br \/>\n\t\t\t&lt;tr&gt;<br \/>\n\t\t\t\t&lt;td valign=&#8221;top&#8221;&gt;&lt;input type=&#8221;checkbox&#8221; value=&#8221;&lt;?php echo $postid; ?&gt;&#8221; \/&gt;&lt;\/td&gt;<br \/>\n\t\t\t\t&lt;td&gt;&lt;?php echo get_post_meta($postid, &#8216;cp_unique_id&#8217;, true); ?&gt;&lt;\/td&gt;<br \/>\n\t\t\t\t&lt;td&gt;&lt;?php echo $user-&gt;display_name; ?&gt;&lt;\/td&gt;<br \/>\n\t\t\t\t&lt;td&gt;&lt;?php echo $post-&gt;post_title; ?&gt;&lt;\/td&gt;<br \/>\n\t\t\t\t&lt;td&gt;&lt;?php echo get_post_meta($postid, &#8216;cp_price&#8217;, true); ?&gt;&lt;\/td&gt;<br \/>\n\t\t\t\t&lt;td&gt;&lt;?php echo date_i18n(&#8220;j \\d\\e F \\d\\e Y \\\u00e0\\s H:i:s&#8221;, strtotime($post-&gt;post_date)); ?&gt;&lt;\/td&gt;<br \/>\n\t\t\t\t&lt;td&gt;&lt;?php echo date_i18n(&#8220;j \\d\\e F \\d\\e Y \\\u00e0\\s H:i:s&#8221;, strtotime(get_post_meta($post-&gt;ID, &#8216;cp_sys_expire_date&#8217;, true))); ?&gt;&lt;\/td&gt;<br \/>\n\t\t\t\t&lt;td&gt;&lt;?php echo $post-&gt;post_status; ?&gt;&lt;\/td&gt;<br \/>\n\t\t\t\t&lt;td&gt;&lt;?php echo $user-&gt;user_email; ?&gt;&lt;\/td&gt;<br \/>\n\t\t\t\t&lt;td&gt;&lt;?php echo get_user_meta($post-&gt;post_author, &#8216;user_tel_fixo&#8217;, true); ?&gt;&lt;\/td&gt;<br \/>\n\t\t\t\t&lt;td&gt;&lt;?php echo get_post_meta($postid, &#8216;cp_city&#8217;, true); ?&gt;&lt;\/td&gt;<br \/>\n\t\t\t\t&lt;td&gt;&lt;?php echo get_post_meta($postid, &#8216;cp_state&#8217;, true); ?&gt;&lt;\/td&gt;<br \/>\n\t\t\t\t&lt;td&gt;&lt;?php echo $categories[0]-&gt;cat_name; ?&gt;&lt;\/td&gt;<br \/>\n\t\t\t&lt;\/tr&gt;<br \/>\n\t\t\t&lt;?php endforeach; ?&gt;<\/p>\n<\/blockquote>\n","protected":false},"template":"","class_list":["post-31735","topic","type-topic","status-closed","hentry","topic-tag-categoria"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/br.wordpress.org\/support\/wp-json\/wp\/v2\/topic\/31735","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/br.wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/br.wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/br.wordpress.org\/support\/wp-json\/wp\/v2\/topic\/31735\/revisions"}],"wp:attachment":[{"href":"https:\/\/br.wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=31735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}