This is what I have tried so far:
<?php /* Template Name: Archive-posts */ ?>
<?php get_header(); ?>
<main id="primary" class="site-main">
<div class="page-header" id="page-header=news">
<h1>Archive</h1>
</div>
<div class="archived">
<div class="archive-grid">
<!-- Query list of archive posts -->
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'archive',
'posts_per_page' => 12, // Change this to the number of posts you want to display per page
'paged' => $paged,
'meta_key' => 'event_date', // Specify the meta key
'orderby' => 'meta_value', // Order by the meta value
'order' => 'DESC', // Order in descending order
);
query_posts($args);
if (have_posts()) :
while (have_posts()) : the_post();
?>
<a href="<?php the_permalink(); ?>" class="archive-item">
<h2><?php the_title(); ?></h2>
<?php
// Get the event_date custom field value
$event_date = get_post_meta(get_the_ID(), 'event_date', true);
if ($event_date) {
// Convert the date to a more readable format
$formatted_date = date('F j, Y', strtotime($event_date));
echo '<div class="event-date">' . esc_html($formatted_date) . '</div>';
}
?>
</a>
<?php endwhile; ?>
</div>
<div class="pagination-news">
<?php echo paginate_links(array('total' => $wp_query->max_num_pages)); ?>
</div>
<?php endif; ?>
</div>
</main>
<?php wp_reset_postdata(); // Reset the post data ?>
<?php get_footer(); ?>
tried to fix following this question, but couldn't sort it: Custom post type pagination not working in index.php file
$query->set( 'property', value );
multiple times, and just set the same values you are using in your custom query now.query_posts()
.