0

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

5
  • Please show us what exactly you have tried, so we don't have to repeat code unnecessarily. The answer in the linked post is the correct way to solve your problem.
    – fuxia
    Commented Mar 2 at 19:44
  • Thanks for your answer. I understood that I should modify my main query. But how to do it? I tried to follow the answer linked but instead gave me all the posts despite the custom category. Commented Mar 3 at 18:28
  • Use $query->set( 'property', value ); multiple times, and just set the same values you are using in your custom query now.
    – fuxia
    Commented Mar 3 at 19:24
  • as a function in the function.php files? function custom_post_type_archive_pagination( $query ) { if ( ! is_admin() && $query->is_main_query() && is_post_type_archive( 'archive' ) ) { $query->set( 'paged', get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1 ); $query->set( 'posts_per_page', 12 ); } } add_action( 'pre_get_posts', 'custom_post_type_archive_pagination' ); Commented Mar 3 at 20:01
  • 1
    Don't use query_posts().
    – Pat J
    Commented Mar 3 at 23:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.