1

Have a strange problem with pagination, the query work fine when I'm in homepage, but if click for the next page I get a 404 error. Disabling permalinks settings, I saw that in the query string (when I move on to the next page), then I have ?paged=2 as query argument, but not post_type=job_listing (in this case). Here is my code:

global $wp_query, $paged;

if ( get_query_var( 'paged' ) )
    $paged = get_query_var( 'paged' );
else
    $paged = 1;

if ( get_query_var( 'post_type' ) )
    $post_type = get_query_var( 'post_type' );
else
    $post_type = 'job_listing';

$args = array(
   'post_type' => $post_type,
   'paged'     => $paged,
);

query_posts( $args );

And then I have a normal loop cycle and a wp_reset_query() at the end. The problem is that when open a pagination link the url look like www.example.com/?paged=2 and not www.example.com/?post_type=job_listing&paged=2 which works if I type it manually.

I tried everything to make it work but it keeps ignoring the post_type var in all pages.

0

I think your problem is at the following lines:

if ( get_query_var( 'post_type' ) )
    $post_type = get_query_var( 'post_type' );
else
    $post_type = 'job_listing';

Your post_type var will probably already being set, so you are calling some different post type (the one stored inside the query_vars array with the post_type key), which maybe does not have a second page and therefore leads you to your 404. You can simply

var_dump( $GLOBALS['wp_query'] );

or

printf( '<pre>%s</pre>', var_export( $GLOBALS['wp_query'], TRUE ) );`

depending on your debugging setup and take a look at the output to see what goes wrong in detail.

Aside from that, I'd

  1. Use get_posts() or new WP_Query() to not mess up the main query (query_posts() is a private function reserved for core usage).
  2. Set the query var fixed for that query:

    $post_type = 'job_listing';
    
  • 1
    Or just use archive-{post-type}.php, and not worry about custom-loop pagination issues. – Chip Bennett Apr 18 '14 at 13:51
  • @ChipBennett You know that this should be an answer, right? At least I know it's the answer :) – kaiser Apr 18 '14 at 18:09
  • I think it is, yes. But I'm still waiting for a response to my comment above, so that I can post it as the correct answer. – Chip Bennett Apr 18 '14 at 20:08

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.