Search code examples
phpwordpresstemplatesposts

Wordpress - post_category shows no result


i am trying to create a small overview over the latest posts of my page. Everything works well with the "wp_get_recent_posts" query. Now i was trying to add some icons to the title, but it always shows me no result, as soon as i try to get the post_category of a post.

If tried to change the $args 'category' => to '1,2,3,4,...' but it didn't helped.

Any advice is highly appreciated. My code:

                    <?php

                        $args = array(
                            'numberposts' => 5,
                            'offset' => 0,
                            'category' => '',
                            'orderby' => 'post_date',
                            'order' => 'DESC',
                            'include' => '',
                            'exclude' => '',
                            'meta_key' => '',
                            'meta_value' =>'',
                            'post_type' => 'post',
                            'post_status' => 'draft, publish, future, pending, private',
                            'suppress_filters' => true
                        );

                        $recent_posts = wp_get_recent_posts( $args, ARRAY_A );

                        foreach($recent_posts as $post):
                            if($post['post_category'] == 1):
                                echo "<p><i class=\"fas fa-book\">&nbsp;&nbsp;</i>{$post['post_title']}<br></p>";
                            elseif($post['post_category'] == 2):
                                echo "<p><i class=\"fas fa-desktop\">&nbsp;&nbsp;</i>{$post['post_title']}<br></p>";
                            endif;
                            echo $post['post_category']; //Debug, no output.
                            echo $post['post_title']; //Debug, output: "example post"
                            echo $post['post_date']; //Debug, output: "2019-05-21"
                        endforeach;

                        ?>

Solution

  • The post object does not have a post_category property: https://codex.wordpress.org/Class_Reference/WP_Post

    You could use get_the_category function with the post ID (https://developer.wordpress.org/reference/functions/get_the_category/):

    <?php    
    
                            $args = array(
                                'numberposts' => 5,
                                'offset' => 0,
                                'category' => '',
                                'orderby' => 'post_date',
                                'order' => 'DESC',
                                'include' => '',
                                'exclude' => '',
                                'meta_key' => '',
                                'meta_value' =>'',
                                'post_type' => 'post',
                                'post_status' => 'draft, publish, future, pending, private',
                                'suppress_filters' => true
                            );
    
                            $recent_posts = wp_get_recent_posts( $args, ARRAY_A );
    
                            foreach($recent_posts as $post):
    
                                $categories = get_the_category($post['ID']);
    
                                foreach ($categories as $category):
                                    if ($category->cat_name == 'firstcategory'):
                                        //first category found                                  
                                        var_dump($category->cat_name);
                                    endif;
                                    if ($category->cat_name == 'secondcategory'):
                                        //second category found
                                        var_dump($category->cat_name);
                                    endif;
                                endforeach;
    
                            endforeach;
                            ?>