• Resolved admiralchip

    (@admiralchip)


    Hi everyone!
    I’m building a custom plugin to store numbers in a database table. Afterwards, the numbers will be outputted on a page in the admin dashboard. The page in question has a url structure that is something like this: “https://localhost/mysite/wp-admin?page=my-slug&op=view&id=1”. Everything was going fine until I tried adding pagination to the query results. I used paginate_links() to add the links. When I put the cursor on the page links while on page 1, the page numbers in the links are correct, ie. pagenum=1, pagenum=2. So when i click on “next” or page 2 “&pagenum=2” gets appended to the above url (which is correct). However when i’m on page 2 and i put the cursor on the page links, all the pages are linked as pagenum=2 so consequently clicking on the link to page 3 still takes me to page 2. It’s really strange because I used paginate_links() before on the frontend and it worked perfectly. Do I need to do something else since the page is in the admin dashboard?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    paginate_links() assumes that it is called on a page that has gone through the standard frontend query parsing. It pulls values from that query that dictate how it works. Backend pages do not necessarily go through the same process and the function may be pulling values that either do not exist or contain the wrong values.

    You can still use the function, but you need to feed it the correct values in its arguments instead of relying on the query to feed it. You mainly need to be concerned with the ‘total’ and ‘current’ values.

    I was initially confused by your use of the term ‘dashboard’. For most people in these forums, we think of the ‘dashboard’ as only the one page you get after login that has the various meta boxes (At a Glance, Activity, etc.) and actually is labeled ‘Dashboard’ at the top. Since you have multiple pages I can’t see how this occurs in one of the dashboard metaboxes. I’m thus assuming your pages occur in the more general ‘Admin Area’ or ‘Backend’.

    Not a big deal, you can call it whatever you like amongst friends, but by using consistent terminology when communicating with strangers you’ll minimize confusion.

    Thread Starter admiralchip

    (@admiralchip)

    Thanks for the reply and sorry for the wrong use in terminology. I indeed meant a page that is in the admin backend. The page is one that is created by my custom plugin.

    I followed the example that was given here by Hemi: https://stackoverflow.com/questions/5322266/add-pagination-in-wordpress-admin-in-my-own-customized-plugin

    You can still use the function, but you need to feed it the correct values in its arguments instead of relying on the query to feed it. You mainly need to be concerned with the ‘total’ and ‘current’ values.

    So should it look something like this instead?

    $page_links = paginate_links( array(
        'base' => '%_%',
        'format' => '&pagenum=%#%',
        'prev_text' => __( '«', 'text-domain' ),
        'next_text' => __( '»', 'text-domain' ),
        'total' => $num_of_pages,
        'current' => $pagenum
    ) );
    
    if ( $page_links ) {
        echo '<div class="tablenav"><div class="tablenav-pages" style="margin: 1em 0">' . $page_links . '</div></div>';
    }

    Thanks in advance.

    Moderator bcworkz

    (@bcworkz)

    Yes, that should do it. Of course success depends on ensuring $pagenum has the correct value.

    Thread Starter admiralchip

    (@admiralchip)

    Thanks for the reply. I tried putting it that way but unfortunately it didn’t work as well.

    I eventually sorted it out by building a custom function to generate pagination. ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Pagination links for custom query in admin dashboard show the same page number’ is closed to new replies.