Site icon Hip-Hop Website Design and Development

Why does my WP_Query pagination on a custom page.php only loads the homepage?

What I am looking to do:

Setup WP_Query pagination in a custom php template file with a custom WP_Query query (It’s not originally mine, I’m just fixing it). The file loops through posts (category "Publishing") and adds them to the page (5 at once).

My code should work it seems to be very well done and follows the WP documentation. I wonder what bug is stopping it from working.

What I have done

  1. Tried to add the pagination myself with paginate_links(array). I did get the pagination bar but every time I clicked a link it just jumped back to the homepage. But there was a /page/2/ etc. added to the url.

  2. Tried the WP-PageNavi plugin added:

$custom_query->query(array('category_name' => 'Publishing', 'page' => get_query_var('paged')));

and used the shortcode:

wp_pagenavi(array( 'query' => $custom_query ) );

Result is the same like in 1). It always jumps back to the homepage but adds the slug to the URL.

What I am trying to do

Add a pagination at the bottom of the page that works. I guess there might be some problem with how the loop is working in my template?

My current code with the WP-PageNavi plugin:

<?php

$args = array(
    
    "category_name"    => "Publishing",
    "orderby"          => "post_date",
    "order"            => "DESC",
    "post_type"        => "post",
    "post_status"      => "publish",
    );

$custom_query = new WP_Query($args); 
$custom_query->query(array('category_name' => 'Publishing', 'page' => get_query_var('paged')));


if ( $custom_query->have_posts() ) :
    while ( $custom_query->have_posts() ) : $custom_query->the_post(); 

         
        { $id=$post->ID; $image=get_the_post_thumbnail($id, 'full'); 
            if( class_exists('Dynamic_Featured_Image') ) {
                global $dynamic_featured_image;
            
                $featured_images = $dynamic_featured_image->get_featured_images($id );
                
            }

                echo "<div class='direkt'>".get_the_date( 'd F Y', $post->ID )."<a href=".get_permalink($post->ID)."?kat=publishing> [Direct Link] </a></div>";
                ?>
                    <div class="custompost_wrapper">
                        <div class="custompost">
                            <div class="splide">
                                <div class="splide__track">
                                    <ul class="splide__list">
                                        <li class="splide__slide">
                                            <?php echo $image; ?>
                                        </li>
                                        <?php foreach($featured_images as $featured_image): ?>
                                            <li class="splide__slide">
                                                <img src="<?php echo $featured_image['full']; ?>"
                                                data-splide-lazy="<?php echo $featured_image['full']; ?>"
                                                loading="lazy"
                                                >
                                            </li>
                                        <?php endforeach; ?>
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </div>

            <?php

            echo "<div class='custompost_contentwrapper'><strong>";
                the_title();
            echo "</strong><br>";
            echo "<div class='thecontent'>";
                the_content();
            echo "</div>";
            echo "<div class='spacer'></div></div>";
        } 
            

            
    endwhile;  

    wp_pagenavi(array( 'query' => $custom_query ) );
    
    wp_reset_postdata();
endif;

?>

The method I tried before the plugin:

<?php

$published_posts = wp_count_posts()->publish;
$posts_per_page = get_option('posts_per_page');
$page_number_max = ceil($published_posts / $posts_per_page);

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
    
    "category_name"    => "Publishing",
    "orderby"          => "post_date",
    "order"            => "DESC",
    "post_type"        => "post",
    "post_status"      => "publish",
    );

$custom_query = new WP_Query($args); 


if ( $custom_query->have_posts() ) :
    while ( $custom_query->have_posts() ) : $custom_query->the_post(); 

         
        { $id=$post->ID; $image=get_the_post_thumbnail($id, 'full'); 
            if( class_exists('Dynamic_Featured_Image') ) {
                global $dynamic_featured_image;
            
                $featured_images = $dynamic_featured_image->get_featured_images($id );
                
            }

                echo "<div class='direkt'>".get_the_date( 'd F Y', $post->ID )."<a href=".get_permalink($post->ID)."?kat=publishing> [Direct Link] </a></div>";
                ?>
                    <div class="custompost_wrapper">
                        <div class="custompost">
                            <div class="splide">
                                <div class="splide__track">
                                    <ul class="splide__list">
                                        <li class="splide__slide">
                                            <?php echo $image; ?>
                                        </li>
                                        <?php foreach($featured_images as $featured_image): ?>
                                            <li class="splide__slide">
                                                <img src="<?php echo $featured_image['full']; ?>"
                                                data-splide-lazy="<?php echo $featured_image['full']; ?>"
                                                loading="lazy"
                                                >
                                            </li>
                                        <?php endforeach; ?>
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </div>

            <?php

            echo "<div class='custompost_contentwrapper'><strong>";
                the_title();
            echo "</strong><br>";
            echo "<div class='thecontent'>";
                the_content();
            echo "</div>";
            echo "<div class='spacer'></div></div>";
        }   

            
    endwhile;  
    
    $total_pages = $page_number_max;

    if ($total_pages > 1){
        $current_page = max(1, get_query_var('paged'));
                
        echo paginate_links(array(
            'base' => get_pagenum_link(1) . '%_%',
            'format' => 'page/%#%',
            'current' => $current_page,
            'total' => $total_pages,
            'prev_text'    => __('« prev'),
            'next_text'    => __('next »'),
        ));
    };
    
    wp_reset_postdata();
endif;

?>

Any help would be appreciated. I’m trying to get the pagination to work for hours now.