Support

Account

Home Forums General Issues Pagination when posts ordered by custom field

Helping

Pagination when posts ordered by custom field

  • I am trying to add pagination to a WordPress blog page where the query contains an ACF custom Field and my posts are ordered by this custom field: event_date. The code for pagination works fine on normal query, but not here. Can’t see what’s causing issue here. The pagination code just gives empty div container. (I have a code for pagination in my functions.php)

    <?
    $paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1; 
    $args = array(
        'paged' => $paged,
        //'posts_per_page'=>5,
        'meta_key' => 'event_date', // name of custom field
        'orderby' => 'meta_value_num',
        'order' => 'DSC',
        'cat' => '2'
    );
    
    $the_query = new WP_Query( $args ); 
         if ( $the_query->have_posts() ) {
            while ( $the_query->have_posts() ) { 
    ?>          
            <?php $the_query->the_post(); ?>
    
                    <li id="post-<?php the_ID(); ?>">
                            <?php 
                                $date = get_field('event_date');
                                $dateformat = date("F j, Y", strtotime($date));
                            ?>
                            <p class="date"><?php echo $dateformat; ?> 
    
                            <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
                                <a href="<?php the_permalink() ?>"><?php the_post_thumbnail( 'thumb-270x270' ); ?></a>
                                <?php the_tags( '', ', ', ''); ?>
    
                    </li>
        <?php } } else { ?>
            <h2>Not Found</h2>
        <?php } ?>
    
          <div id="pagination"><?php if (function_exists("pagination")) {
            pagination($additional_loop->max_num_pages);
           } ?></div>
    
        <?php wp_reset_postdata(); ?> 
  • I see 2 things in your code

    The first doesn’t effect the pagination but may be effecting the order 'order' => 'DSC', should be 'order' => 'DESC',

    The other is this line pagination($additional_loop->max_num_pages); What is $additional_loop? shouldn’t that be pagination($the_query->max_num_pages);

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Pagination when posts ordered by custom field’ is closed to new replies.