0

First sorry for my english, is not my first language.

So what I want to do is to get the products of a particular page, using the get variable, so this is my current code...

$page = 0;
if(isset($_GET['pagina'])){
    $page = $_GET['pagina'];
}
$args = array( 'post_type' => 'product','posts_per_page' => 1, 'page' => $page);
$loop = new WP_Query( $args );

while ( $loop->have_posts() ) : $loop->the_post(); 
    global $product; 

    echo get_the_title();
endwhile;

I though the parameter 'page' => $page will do exactly that... but it seems I was wrong and maybe I understood it wrong when I was reading it.

So example if I have 2 products: product_1 and product_2 and my posts_per_page is 1 what Im trying to archive is that

if I type http://myurl.com/?pagina=1 will show product_1

and if I type http://myurl.com/?pagina=2 will show product_2

Thanks in advance


EDIT: I think my english failed to communicate my problem so ima try explaning in other example

What I want is to get the products that are suppose to go in the page I get tru $_GET So if I have to use my own pagination, it will look something like this:

$page = 1;
$posts_per_page = 1;
if(isset($_GET['pagina'])){
    $page = $_GET['pagina'];
}
$start_from = $posts_per_page * ($page-1);
$sql = "SELECT * FROM Products LIMIT $start_from,$posts_per_page"
  • Use page_id, not page (if the ID is the parameter you are getting from the pagina var. codex.wordpress.org/Class_Reference/WP_Query – The J Mar 14 '17 at 4:26
  • @TheJ the parameter im getting from the GET is the current page number of the pagination, I would like to retrieve the products that are in that page, in my example I use posts_per_page => 1 but I wouldn't use that, it was just a example, so what im trying to say is if GET['pagina'] == 2 then get the products on page 2, if would implement this myself, the query will look something like $start_from = ($page-1) * $posts_per_page; $sql = "SELECT * FROM Products LIMIT $start_from,$posts_per_page"; – centenond Mar 14 '17 at 4:40
  • @TheJ I edited my question, in the comments its hard to read the code – centenond Mar 14 '17 at 4:44
  • Ah I see what you want to do. That's definitely not the right way to go about it. Also, direct SQL calls are almost never needed. I'd build a simple WP Query, using the offset (that you can calculate dynamically) parameter and post_type = 'product'. So for each page, then you get your offset value with the number of posts_per_page * page. – The J Mar 14 '17 at 4:45
  • @TheJ I think that this offset parameter answers all my questions, didn't know there was one, Ill give it I try and come back – centenond Mar 14 '17 at 4:49
0

Build a simple WP Query, using the offset parameter and post_type => 'product'.

So for each page, you calculate your offset value with $posts_per_page * $page.

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.