1

How do I pass additional parameter in the url for pagination to work

my existing page link is http://localhost/site/wp-admin/admin.php?page=myPage

How do I create the syntax / logic in PHP to add &pagenum=1 in the url?

currently I have this code

global $wpdb;

$per_page = 6;
$page_query = $wpdb->get_var("SELECT COUNT('id') FROM form"); 

$pages = ceil($page_query / $per_page);

$currentPage = (isset ($_GET['pagenum'])) ? (int)$_GET['pagenum'] : 1;
$start = ($currentPage -1 ) * $per_page;

$row = $wpdb->get_results("SELECT * FROM form LIMIT $start , $per_page");

//foreach loop here
1

There is a wordpress function for adding $_GET-Params to an URL: add_query_arg(). It works like this:

$link = add_query_arg( array('pagenum' => $your_page_number) );

By default it adds the param to the current page (gets content from $_SERVER[REQUEST_URI]) but you can also pass an optional param to the function if you want the link to go to another page:

$link = add_query_arg( array('pagenum' => $your_page_number), $your_page_link);
  • hi, it appears this is the one im looking for, but not sure where to actually include this. do i have to create a condition if (isset($_GET['page']) == 'myPage') { add_query_arg( 'foo', 'bar' ); }? – user1645034 Sep 18 '13 at 7:27
  • You'll have the actual link stored in $link afterwards. It's hard to say what you should do with it as this context is missing in your question, but basically you'll want to output it somewhere so the user clicks on it and the $_GET param will then be set on that page-load. You are mixing two things up a bit: First set the $_GET var, then retreive the $_GET var. This happens in two different page lods. – s1lv3r Sep 18 '13 at 9:07

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.