Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Pagenavi Plugin and Custom Post Types (Navigation Error) WordPress

  • SOLVED

Greetings!

I have an issue with paging if I try to navigate to the second page (and all the rest pages).



After a lot of search I came to the conclusion that this issue is due to the way paging work and something with the custom query. It seems that it cannot find pages 2,3 and so on. I get the 404.php template instead. However I haven't found a solution (tried to reset the query or to "duplicate" the custom query parameters) yet.

My code is



<?php
$my_query = new WP_Query( array( 'post_type' => 'gallery', 'paged' => get_query_var('paged'), 'showposts' =>32 ) );
if ( $my_query->have_posts() ) : while ( $my_query->have_posts() ) : $my_query->the_post(); ?>

...stuff here....

<?php endwhile; endif; ?>


<?php
wp_pagenavi( array( 'query' => $my_query ) );
wp_reset_postdata();
?>



Any help would be appreciated

Answers (1)

2011-03-02

Oleg Butuzov answers:

query results should work before tempaltes loaded.

it dosn't work in way you want to do that...


Panagiotis Grigoropoulos comments:

Actually this post [http://scribu.net/wordpress/wp-pagenavi/wpn-2-74.html] shows that the plugin works exactly how I want it to work. Even the query is similar. However I can't understand why it comes up with 404.php template.

Any ideas on how to fix this?


Oleg Butuzov comments:

let me tell you ...

whan you click page 2 -
its tries to get results before load template, and is there no results... you will get 404.


what i am doing - ia m creatign custom urls for custom post types so its easy fit for paging structure.... i can provide example simple rewrite rules query if you want... but it will work only if you are underestanding right now ehre is a problem.


Panagiotis Grigoropoulos comments:

I see. Well I do understand what you're saying.
Is this way going to mess any other links on the website? Or just the custom post type's urls?


Oleg Butuzov comments:

as for me i am deleting post type urls created by system...


just a small example

add_filter('rewrite_rules_array', 'rewrite_rules_array_procurement', 10000);
function get_matches_rules(){
$custom = array(
get_option('procurement_base').'/([^/]+)?$' => 'index.php?post_type=procurement&procurement=$matches[1]',
get_proption('ocurement_base').'([^/]+)?$' => 'index.php?post_type=procurement',
get_option('procurement_base').'/page/?([0-9]{1,})/?$' => 'index.php?post_type=procurement&paged=$matches[1]',
);

return $custom;
}

function rewrite_rules_array($rules){



foreach($rules as $k=>$i){

if ('procurement' == substr($k,0,strlen('procurement'))){
unset($rules[$k]);
}
}


$keys = array_keys($rules);
$values = array_values($rules);

$custom = get_matches_rules();
$custom_keys = array_keys($keys);
$custom_values = array_values($custom);

foreach($custom as $rule=>$match){
array_unshift($keys, $rule);
array_unshift($values, $match);
}

$rules = array_combine($keys, $values);



return $rules;
}


get_proption('procurement_base') in a static way to the procurements section...
like a page url (in this example it about/procurements)



and at the url about/procurements i am getting the full featured query loop. so i don't need to make custom query... just having_posts()...


Panagiotis Grigoropoulos comments:

Ok some clarification please

This code goes to functions.php right?
The custom post type in your example is "procurements" ?

How do you query posts after all that?


Oleg Butuzov comments:

1) yeah, its can be fucntions.php or any plugin...
2) yes.
3) wp does.

as i said - you don't need to query posts... take a look to the item s of the array

'index.php?post_type=procurement&procurement=$matches[1]',
'index.php?post_type=procurement',

see ? its a query vars that passes to wp query.


all you need ot do is use while(have_posts) /*normal wp loop*/