theme development - i cant see the max_num_pages of a custom wp query

admin2025-04-20  0

i hope you help me with this,

i am making a wordpress website witch has 3 diffrent post types(the regular post type and 2 others), i have the pagenation work very with in the regular posts page, but it does not work in the other 2 page of the other custom posts, when i tried to solve this out i found that the problem is that like the custom query object does not have the max_num_pages attribute, when i echo the value of it, it does not show of , look at my code!

this is the custom query i made:

                            $currentpage = get_query_var('paged');
                            $myQuery= new WP_Query(array(
                                    'post_type'         => 'airdrop',
                                    'posts_per_page'    => 3,
                                    'paged'             => $currentpage 
                            ))


                                 function my_pagination(){
                                    $all_pages = $myQuery->max_num_pages;
                                    $current_page = max(1 , get_query_var('paged'));
                                    echo $current_page ."/". $all_pages;
                                    if ($all_pages>1) {
                                        return paginate_links(array(
                                            'base'      =>get_pagenum_link() .'%_%',
                                            'format'    =>'page/%#%',
                                            'current'   =>$current_page
                                        ));
                                    }
                                }
                                echo my_pagination();

if you look at this line echo $current_page ."/". $all_pages; this should print something like 1 / 2 or 1 /3 , but it shows only 1 / blank and the num_max not shwing, i hope you help me figure it out

EDIT :

even this simple codeisn't working

                           $loop = new WP_Query(array(
                                    'post_type'         => 'airdrop',
                                    'posts_per_page'    => 3,
                            ));
                            echo $loop->max_num_comment_pages;

i hope you help me with this,

i am making a wordpress website witch has 3 diffrent post types(the regular post type and 2 others), i have the pagenation work very with in the regular posts page, but it does not work in the other 2 page of the other custom posts, when i tried to solve this out i found that the problem is that like the custom query object does not have the max_num_pages attribute, when i echo the value of it, it does not show of , look at my code!

this is the custom query i made:

                            $currentpage = get_query_var('paged');
                            $myQuery= new WP_Query(array(
                                    'post_type'         => 'airdrop',
                                    'posts_per_page'    => 3,
                                    'paged'             => $currentpage 
                            ))


                                 function my_pagination(){
                                    $all_pages = $myQuery->max_num_pages;
                                    $current_page = max(1 , get_query_var('paged'));
                                    echo $current_page ."/". $all_pages;
                                    if ($all_pages>1) {
                                        return paginate_links(array(
                                            'base'      =>get_pagenum_link() .'%_%',
                                            'format'    =>'page/%#%',
                                            'current'   =>$current_page
                                        ));
                                    }
                                }
                                echo my_pagination();

if you look at this line echo $current_page ."/". $all_pages; this should print something like 1 / 2 or 1 /3 , but it shows only 1 / blank and the num_max not shwing, i hope you help me figure it out

EDIT :

even this simple codeisn't working

                           $loop = new WP_Query(array(
                                    'post_type'         => 'airdrop',
                                    'posts_per_page'    => 3,
                            ));
                            echo $loop->max_num_comment_pages;
Share Improve this question edited Oct 13, 2019 at 17:42 bilal BOUASRIA asked Oct 12, 2019 at 19:10 bilal BOUASRIAbilal BOUASRIA 131 silver badge5 bronze badges 3
  • $myQuery is defined outside and isn't accessible from within my_pagination(). – Sally CJ Commented Oct 12, 2019 at 22:20
  • why isn't accessible, it's global – bilal BOUASRIA Commented Oct 13, 2019 at 17:31
  • I would suggest you to change your pagination function to support custom WP_Query objects/instances like this - and in your case, you could simply change your function to function my_pagination( $myQuery ) and then when you call the function, supply the WP_Query instance like so: my_pagination( $myQuery ). And looking at your current answer, that's an example problem of using global variables - when the same variable is assigned to another WP_Query instance, then you'd run into issues. – Sally CJ Commented Oct 14, 2019 at 10:01
Add a comment  | 

2 Answers 2

Reset to default 1

Your variables are not in functions scope.

My example of function:

function customPagination($paged = '', $max_page = '') {
    global $wp_query;
    $big = 999999999;
    if( ! $paged )
        $paged = get_query_var('paged');

    if( ! $max_page )
    $max_page = $wp_query->max_num_pages;
    echo paginate_links( array(
        'total' => $max_page,
        'prev_next' => false,
        'prev_text'  => false,
        'next_text'  => false,
        'total' => $wp_query->max_num_pages,
        'mid_size' => 3
    ) );
}

and then calling it with your variables (just changed to camelCase)

customPagination($currentPage, $myQuery->max_num_pages); 

Hope it helps

Heading

I found the answer : what i was doing wrong is that i have two wp_query objects with the same name ($loop).

when first i made the two posts type i gave theme same name, this is why it gets confused,

the solution is that never use two or more wp_query with the same name!!!

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745085051a284090.html

最新回复(0)