1

Hi everybody and tks in advance for your help! I have a plugin and I was trying to do some pagination for a custom query. Everything seems to work fine, but when I click on page 2 (or whatever not being "1"), wordpress says

You do not have sufficient permissions to access this page

I believe that this error is because with this pagination method that I was trying, the URL is:

/wp-admin/admin.php?page=lismovim-withdraw-slug/page/2/

So, I suppose that WP doesn't reconigze this address. Any ideas? Maybe some other way to do the pagination? I really need some help, I was surfing the internet quite long now and couldn't find any solution. Here's my code:

function listado_movimientos(){


    echo '<p><strong>LISTADO DE MOVIMIENTOS APROBADOS/DECLINADOS<strong></p>';
    echo '<table class="wp-list-table widefat fixed striped axdepositbycash">
    <tr style="font-weight: bold;font-size: 15px">
                <td>ID</td>
                <td>FECHA</td>
                <td>USUARIO</td>
                <td>NOMBRE</td>
                <td>CANTIDAD</td>
                <td>Nº CUENTA</td>
                <td>ESTADO</td>
            </tr>';

    $rows_per_page = 10;
    $current = (intval(get_query_var('paged'))) ? intval(get_query_var('paged')) : 1;

    global $wpdb; 
    $table_name = $wpdb->prefix. "withdraw";
    $result2=$wpdb->get_results("select * from $table_name where status='aprobado'");


    $pagination_args = array(
        'base' => @add_query_arg('paged','%#%'),
        'format' => '',
        'total' => ceil(sizeof($result2)/$rows_per_page),
        'current' => $current,
        'show_all' => false,
        'type' => 'plain',
        );


        $pagination_args['base'] = user_trailingslashit( trailingslashit( remove_query_arg('s',get_pagenum_link(1) ) ) . 'page/%#%/', 'paged');


        if( !empty($wp_query->query_vars['s']) )
        $pagination_args['add_args'] = array('s'=>get_query_var('s'));

        echo paginate_links($pagination_args);

        $start = ($current - 1) * $rows_per_page;
        $end = $start + $rows_per_page;
        $end = (sizeof($result2) < $end) ? sizeof($result2) : $end;

        echo '<br />';
        echo '<br />';

        for ($i=$start;$i < $end ;++$i ) {
        $r2 = $result2[$i];
        echo  '<tr>
                <td>'.$r2->id.'</td>
                <td>'.$r2->wdate.'</td>
                <td> '.$r2->fname. "  " . $r2->lastname.'</td>
                <td>'.$r2->name.'</td>
                <td>'.$r2->amt.'</td>
                <td>'.$r2->acno.'</td>
                <td>'.$r2->status.'</td>
            </tr>';
        }
    echo "</table>";

}

Here is the original snippet, where I found how to do the pagination in the first place.

1 Answer 1

1

The base argument for paginate_links() should be:

admin_url( 'admin.php?page=lismovim-withdraw-slug&paged=%_%' )

... where %_% is replaced by format, which should be:

'%#%'

... which is replaced by the page number.

3
  • Tks! It works just fine but now I have another issue. The URL seems to work fine (in the permissions "way") but pagination is not working at all. When I try to see the value of this line: $current = (intval(get_query_var('paged'))) ? intval(get_query_var('paged')) : 1; It's always getting me "1", so... that means that "get_query_var" is not working to read de "paged" parameter... Am I doing something wrong here? Tks again!! Commented May 25, 2016 at 20:30
  • No, get_query_var works around $wp_query, which is only prepared/ready to use on posts-related admin pages. Use $paged = isset( $_REQUEST['paged']) ? max( 1, ( int ) $_REQUEST['paged'] ) : 1; Commented May 25, 2016 at 21:11
  • Oh man, I must say you really really save my day. It works exactly as you said !!! Thanks you very much!!! Hope in the future I can help you with something!! Commented May 25, 2016 at 21:18

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.