0

I have a custom query that can a list that can be up to around 20 items, so it needs pagination. It is displaying the correct amount of pages for how many items, but when clicking it it refreshes and the printed page number doesn't change from 1->2. I watched this video and this documentation. but have just been spinning my wheels. This loop is part of a single-item page and we are using the Tainacan theme, which gets really frustrating to work with. This question is outside of the theme, it is just for context. enter image description here

enter image description here

$postType = get_post_type();
$dynamicValue = get_post_meta(get_the_ID(), '3870', true);

// Remove the last characters "%.mp3" from the dynamic value
$dynamicValue = substr($dynamicValue, 0, -6);
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
    'post_type' => array($postType),
    'meta_query' => array(
        array(
            'key' => '3870',
            'value' => $dynamicValue,
            'compare' => 'LIKE',
        )
    ),
    'orderby' => 'meta_value',
    'meta_key' => '3870',
    'order' => 'ASC',
    'posts_per_page' => 10, // Number of items to display per page
    'paged' =>  get_query_var('paged'),
);

$query = new WP_Query($args);

if ($query->have_posts()) {
    echo '<div>' . __('<table class="tg-wrap", id="recordingTracks"> ', 'tainacan-interface');

    echo '<thead><tr><th colspan="2">Tracks from this segment</th></tr></thead><tbody>';

    $part_number = 1;

    while ($query->have_posts()) {
        $query->the_post();
        $meta_print_value = get_post_meta(get_the_ID(), '3870', true);
        $trackNum = substr($meta_print_value, -6, -4);
        $trackNumNumbersOnly = preg_replace('/[^0-9]/', '', $trackNum); // Strip out letters
        $highlightClass = ($sessionId == $meta_print_value) ? 'highlight' : ''; // Check if it matches the session ID


        ?>

        <tr>
            <td class="<?php echo $highlightClass; ?>"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></td>
            <td>Track <?php echo $part_number; ?></td>
            <td><?php echo ($meta_print_value); ?></td>
        </tr>

        <?php
        $part_number++;
    }

    echo '</tbody></table></div>';
   

    **echo $paged;
    echo '<br/>------------------<br/>';
    $big = 999999999; // need an unlikely integer
    
     echo paginate_links(array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format'  => '?page=%#%',
        'current' => get_query_var( 'paged') ,
        'total'   => $query->max_num_pages
    ));**

} else {
    // No posts found
}

wp_reset_postdata();


}
    
    //end if session exists

    // If all three are false, print the metadata
    if (!$recordingSessionExists && !$sessionExists && !$communitiesExists) {
        echo 'Metadata:<br>';
        echo '<pre>';
        print_r($data->metadata);
        echo '</pre>';
    }
}

1 Answer 1

0

I believe the mistake is using get_query_var( 'paged' ): this comes from the main query, not from your custom query. Try this (untested):

$current_page = 1;

if ( isset( $_GET['page'] ) ) {
    $current_page = absint( $_GET['page'] );
}

And then replace uses of get_query_var( 'paged' ) with $current_page.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.