ShoeLace1291
ShoeLace1291

Reputation: 4698

How can I get the latest post from a category in WordPress?

I am designing the category page. I have a loop that displays the child categories for the current category. For each child category, I would like to display a link to the latest article. Currently, the link is the same for all child categories, even when the displayed article is not in that category. What am I doing wrong?

<?php 
     $cat_id = get_query_var('cat');
     $categories = get_categories(array( 'parent' => $cat_id));
            if(count($categories) > 0):
                foreach($categories as $cat): 
                    $args = array(
                        'numberposts' => 1,
                        'offset' => 0,
                        'category' => $cat->cat_ID,
                        'orderby' => 'ID',
                        'order' => 'DESC',
                        'post_type' => 'post',
                        'post_status' => 'publish',
                        'suppress_filters' => true );

                        $the_query = new WP_Query( $args ); 
                        $the_query->the_post();
                        if ( $the_query->have_posts() ) : 
                            while ( $the_query->have_posts() ) : $the_query->the_post(); 
                                $recent['title'] = get_the_title();
                                $recent['id'] = get_the_ID();
                                wp_reset_postdata();
                            endwhile;
                        endif;
                        wp_reset_postdata(); ?>


                <div class="media category-list">
                    <div class="media-body">
                        <div class="details">
                            <h3><a href="<?php echo get_category_link($cat->cat_ID); ?>"><?php echo $cat->name; ?></a></h3>
                            <p><?php echo $cat->description; ?></p>
                        </div>
                        <dl>
                            <dt>Article Total:</dt><dd><?php echo $cat->count; ?></dd>
                            <dt>Last Article:</dt><dd><a href="<?php echo get_permalink($recent["id"]); ?>"><?php echo substr($recent["title"], 0, 48).'...'; ?></a></dd>
                        </dl>
                    </div>
                </div>
                <?php endforeach;
                endif; ?>

enter image description here

Upvotes: 1

Views: 18281

Answers (1)

Sumit
Sumit

Reputation: 1639

Looks like you are using get_posts arguments in WP_Query.

  1. category and numberposts are not valid arguments for WP_Query
  2. Both belongs to get_posts and internally converted to cat and posts_per_page
  3. So when you pass these arguments in WP_Query it does not work. But when you pass WP_Query arguments in get_posts it works ;)

So the updated arguments structure is

$args = array(
    'posts_per_page' => 1,
    'offset' => 0,
    'cat' => $cat->cat_ID,
    'orderby' => 'ID',
    'order' => 'DESC',
    'post_type' => 'post',
    'post_status' => 'publish',
    'suppress_filters' => true 
);

Also please update your query with this

$the_query = new WP_Query( $args ); 
//$the_query->the_post();
if ( $the_query->have_posts() ) : 
    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        $recent['title'] = get_the_title();
        $recent['id'] = get_the_ID();
//        wp_reset_postdata();
    endwhile;
endif;
wp_reset_postdata(); 

the_post should be called only once and after when you are sure that query has posts. wp_reset_postdata hold the data for whole query. So need to do this at end of while loop not inside of the while loop.

Upvotes: 4

Related Questions