0

On my wordpress index, I use a custom query:

$paged = (get_query_var('page')) ? get_query_var('page') : 1;

$project_query = new WP_query(array(
  'post_type' => 'project',
  'paged' => $paged,
  'posts_per_page' => 5
));

I am using wp-paginate to setup page numbering, but regardless, as soon as I try to go to the next page, eg: http://domain/page/2 I get a 404 error.

I really cannot for the life of me figure out what is happening. I've set the wordpress reading option to the same number, and my permalink structure is set to Post name.

Any tips greatly appreciated, I know pagination 404's are a common issue with a lot of people but I haven't been able to find someone who had the same issue with the index.

4
  • this is the wrong way to go about doing this, but in an effort to understand why this doesn't work i ask how many posts of the post type post do you have published? fewer than 6?
    – Milo
    Commented Jun 30, 2013 at 18:13
  • Ah interesting, there isn't a single post published. I see, this is the problem? As wordpress obviously doesn't realize there is another page of custom posts? Should I be creating a static page that replaces the homepage instead?
    – waffl
    Commented Jun 30, 2013 at 18:17
  • Ok, you are right. After creating a custom template and setting a static page as the frontpage, it works now :) Thank you so much!
    – waffl
    Commented Jun 30, 2013 at 18:27
  • see my answer for another alternative that modifies the main query without having to use a static page and custom query.
    – Milo
    Commented Jun 30, 2013 at 18:28

1 Answer 1

2

whether or not a page exists or results in a 404 is decided based on the results of the main query, which happens before the template is loaded. WordPress isn't "aware" of your custom query, it's separate from the main query and happens only if the main query results in the loading of the template which contains it.

instead of creating this custom query, you can modify the main query before it is sent to the database via the pre_get_posts action. this code would go in your theme's functions.php file:

function wpa_front_page_projects( $query ) {
    if ( $query->is_front_page() && $query->is_main_query() ) {
        $query->set( 'post_type', array( 'project' ) );
        $query->set( 'posts_per_page', 5 );
    }
}
add_action( 'pre_get_posts', 'wpa_front_page_projects' );

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.