0

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.

2
  • You query seems to just be for posts belonging to a single category. Is there a reason you're not just using the category archive for this? Dec 8, 2021 at 3:09
  • I just got the page like that. I didn't want to set it up all again so I thought I can just fix it. But yes that would've been a better solution and I could've tried to create a gutenberg block with the slider-code instead.
    – Lisa
    Dec 9, 2021 at 9:59

1 Answer 1

0

You can paginate your custom loop on a page created inside WordPress, the issue with your code is that your are using "page" parameter instead of "paged" parameter; the former is used to paginate a single post parts, while the later is used to paginate post archives or multiple posts.

So a paginated query on your page template should be something like that:

$args = array(

    "category_name"    => "Publishing",
    "orderby"          => "post_date",
    "order"            => "DESC",
    "post_type"        => "post",
    "post_status"      => "publish",
    'paged' => get_query_var('paged'), // This is the correct parameter for pagination
);

$custom_query = new WP_Query($args);

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

        echo '<h1>' . get_the_title() . '</h1>';
        // The remaining of your code here
    endwhile;

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

    wp_reset_postdata();
endif;

Also, I see in your code that you are using "$post" variable without being defined. If you are trying to access the current post in your custom loop, it is better to use the template tags -- helper methods-- e.g. get_the_ID(), get_the_title(),...etc those methods can access the global $post object by default without passing it as a parameter because you have already set it using $custom_query->the_post();

1
  • Thank you, that cleared it up a lot for me. The page was set up like that with passing the $post object as a parameter. But I did get it to work now with your help. Thanks a lot!
    – Lisa
    Dec 9, 2021 at 10:02

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.