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;
  • $myQuery is defined outside and isn't accessible from within my_pagination(). – Sally CJ Oct 12 at 22:20
  • why isn't accessible, it's global – bilal BOUASRIA Oct 13 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 Oct 14 at 10:01
0

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 don't think so, because the $loop object is global in the file, so any function in the file could use it – bilal BOUASRIA Oct 13 at 17:26
  • i tryed it it's not working eather – bilal BOUASRIA Oct 13 at 17:29
0

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!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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