Site icon T4Tutorials.com

How to show post in category in last modified date order?

To show post in category in last modified date order, use this code in your category.php or archive.php file.

<?php
// Get the current category
$category = get_queried_object();

// Custom query to get posts in the current category sorted by last modified date
$query_args = array(
    'category_name' => $category->slug,
    'orderby'      => 'modified', // Sort by last modified date
    'order'        => 'DESC',      // Descending order
    'posts_per_page' => -1          // Show all posts
);

$query = new WP_Query($query_args);

// Output the posts
if ($query->have_posts()) :
    while ($query->have_posts()) : $query->the_post();
        ?>
        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
        <p>Last updated: <?php the_modified_date(); ?></p>
        <?php
    endwhile;
else :
    echo 'No posts found.';
endif;

// Restore original post data
wp_reset_postdata();
?>

 

 

Exit mobile version