0

I'm trying to make a custom PHP pagination work with wordpress, the page in wordpress is a very custom one, not a post or a page, it's a select from DB that shows flowers, the styling is ok now, but it shows all the flowers in a city and i want to limit 10 flowers per page:

$per_page = 10;
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$startAt = $per_page * ($page - 1);

$user_query_subs = new WP_User_Query( array('meta_key' => 'location', 'number' => $per_page, 'offset' => $startAt, 'meta_value'  => $flow_location, 'meta_compare' => '=', 'role' => 'admin', 'orderby' => 'rand', 'order' => 'DESC'), array('fields' => 'all_with_meta') );


$all_user_query_subs = new WP_User_Query( array('meta_key' => 'location', 'meta_value'  => $flow_location, 'meta_compare' => '=', 'role' => 'admin', 'orderby' => 'rand', 'order' => 'DESC'), array('fields' => 'all_with_meta') );

$all_flowers = $all_user_query_subs->get_total();
$flowers = $user_query_subs->get_results();

and after the content is loaded: i'm adding the links on the bottom like this:

<?php
        $totalPages = $all_flowers / $per_page;
        $links = "";
        $current_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        for ($i = 1; $i <= $totalPages; $i++) {
        $links .= ($i != $page ) 
            ? "<a href='" . "$current_url" . "page/$i/'>Page $i</a> "
            : "$page ";
}
    echo $links;
    ?>

The select query is working perfectly, it's loading only 10 per page, but my problem is with the links at the bottom:

Right now when i press the page 2 to access the next 10 flowers from the city, i get Page Not Found from Wordpress but with the correct $current_url/page/$i

Any ideas please? Thanks in advance!

1
1

you can use paginate_links() from wordpress core API, its make more easily to paginate your custom query. And you dont need to make another query to get all users count.

<?php
$users_per_page = 10; // total no of author to display

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if( (int)$paged === 1 ){
  $offset = 0;
} else {
  $offset = ($paged-1) * $users_per_page;
}
$args = array(
    'meta_key' => 'location',
    'number' => $users_per_page,
    'offset' => $offset,
    'meta_value'  => $flow_location,
    'meta_compare' => '=',
    'role' => 'admin',
    'orderby' => 'ID',
    'order' => 'DESC'
);
$fields = array('fields' => 'all_with_meta');
$user_query_subs = new WP_User_Query( $args, $fields );
$users = $user_query_subs->get_results();
if ( !empty( $users ) ) {
    foreach ( $users as $user ) {
        // code goes here
    }
}
else {
    echo 'No flowers found.';
}
?>

And then the pagination, remember it depends on how your structure is.
If you use permalinks, then you can use this code below:

<?php
$query_string = $_SERVER['QUERY_STRING']; // current query parameters
$total_users = $user_query_subs->total_users; // total number of users
$total_pages = ceil( $total_user/$users_per_page ); // total number of pages

// if on the front end, your base is the current page
$base = get_permalink( get_the_ID() ) . '?' . remove_query_arg('paged', $query_string) . '%_%';
$paginate_args = array(  
    'base' => $base,  
    'format' => '?paged=%#%', // define the query parameter that will be used, in this case "paged" 
    'current' => $paged,
    'total' => $total_pages, 
    'prev_text' => 'Previous',  
    'next_text' => 'Next' ,
    'end_size' => 1,
    'mid_size' => 5
);
echo paginate_links($paginate_args); 
?>
1
  • Hullen Thanks for your answer! the URL is converting and working, the links of pagination are showing but it still goes to the NOT FOUND PAGE , i rebuilt the permalinks but no success... any idea?
    – Abude
    Nov 14 '13 at 9:57

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.