Support

Account

Home Forums ACF PRO Pagination not working

Helping

Pagination not working

  • Hi there,

    I’m struggling with querying and pagination. Right now the query works, however the links for the pagination don’t work.

    The previous page link is not appearing and the next page always points to page 2. Any help would be much appreciated.

    <?php
    //global $paged;
    $paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
    $q = filter_input(INPUT_GET, 'q', FILTER_SANITIZE_SPECIAL_CHARS);
    
    if (empty($q)) {
    
        $args = array(
            'post_type'              => 'song',
            'post_status'            => 'publish',
            'meta_key'               => 'song_title',
            'paged'                  => $paged,
            'posts_per_page'         => '15',
        );
    
    } else {
    
        wp_die();
    
        $args = array(
            'post_type'              => 'song',
            'post_status'            => 'publish',
            'meta_key'               => 'song_title',
            'paged'                  => $paged,
            'posts_per_page'         => '15',
            'meta_query'	=> array(
                'relation'		=> 'OR',
                array(
                    'key'		=> 'song_title',
                    'value'		=> $q,
                    'compare'	=> 'LIKE'
                ),
                array(
                    'key'		=> 'artist_name',
                    'value'		=> $q,
                    'compare'	=> 'LIKE'
                )
            )
        );
    
    }
    
    // The Query
    $song_query = new WP_Query( $args );
    
    ?>
    
    <form class="form-inline" method="GET" >
        <input id="q" type="text" class="form-control mb-2 mr-sm-2 mb-sm-0" name="q" placeholder="Search for Artist or Song" value="<?= $q ?>">
    
        <button type="submit" class="btn btn-primary">
            Search
        </button>
    
        <button class="btn btn-default" id="btnClear">
            Clear
        </button>
    </form>
    
    <?php if ( $song_query->have_posts() ) : ?>
    
    <table class="table table-hover table-striped">
        <thead>
            <tr>
                <th scope="col">Song Name</th>
                <th scope="col">Artist Name</th>
                <th scope="col"></th>
            </tr>
        </thead>
        <tbody>
        <?php while ( $song_query->have_posts() ) : $song_query->the_post(); ?>
            <tr>
                <td>
                    <? the_field('song_title'); ?>
                </td>
                <td>
                    <? the_field('artist_name'); ?>
                </td>
                <td>
                    <button type="button" class="btn btn-outline-primary btn-sm request-button" data-songname="<? the_field('song_title'); ?>" data-songartist="<? the_field('artist_name'); ?>" data-songid="<?=get_the_ID() ?>">
                        Request This Song
                    </button>
                </td>
            </tr>
    
    <?php endwhile; ?>
    
        </tbody>
    </table>
    
    <div class="btn btn-default btn-sm"><?= get_next_posts_link( 'Next Page', $song_query->max_num_pages ); ?></div>
    <div class="btn btn-default btn-sm pull-right"><?= get_previous_posts_link( 'Previous Page' ); ?></div>
    
    <?php else:  ?>
    
        <?php _e( 'Sorry, no songs matched your criteria.' ); ?>
    
    <?php endif; ?>
    
    <?php wp_reset_postdata(); ?>
  • This is a WP issue in my opinion, not an ACF issue, so I would recommend turning to the WP community forums for this.

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

The topic ‘Pagination not working’ is closed to new replies.