Community Forums › Forums › Archived Forums › General Discussion › Make pagingation works on custom post type archive
- This topic has 18 replies, 4 voices, and was last updated 8 years, 7 months ago by
Grégoire Noyelle.
-
AuthorPosts
-
October 17, 2014 at 1:17 am #128098
Grégoire Noyelle
MemberHi
I need to have a custom loop custom post type (CPT) archive with pagination.
On the CPT, I have activate genesis archive support. The loop wooks but for the pagination on page 2, I get a 404.Here is my code for the archive-cpt.php file:
<?php // custom loop for cpt archive remove_action( 'genesis_loop', 'genesis_do_loop'); add_action('genesis_loop','gn_loop_event'); function gn_loop_event() { $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $args = array( 'post_type' => 'gnpost_evenement', 'posts_per_page' => 4, 'paged' => $paged ); global $wp_query; $wp_query = new WP_Query( $args ); if (have_posts() ) : while ( have_posts() ) : the_post(); $boucle = new BBPostEvent('event-grid'); endwhile; do_action( 'genesis_after_endwhile' ); wp_reset_query(); endif; } // function gn_loop_event() genesis();
Notes:
the $boucle variable display all the element of the class
And the site is closed. You can use
id: visit
psw: visitCould you help me?
Thanks
http://treow.fr/dev/boumbang/evenement/
GrégoireOctober 17, 2014 at 1:30 am #128103Grégoire Noyelle
MemberI forget to mention that the real name of the custom archive page is : archive-gnpost_evenement.php
October 17, 2014 at 5:26 am #128122David Decker
MemberCou can try to first remove the Genesis pagination and later hook it in again.
Try this - before the remove loop stuff:
/** Remove Genesis pagination - only use pagination on custom loop */ remove_action( 'genesis_after_endwhile', 'genesis_posts_nav' );
Then re-hook it below in your code - try the proper position before or after endif or reset query:
/** Genesis navigation */ genesis_posts_nav();
This thing is untested with your code, but I had similar issues with a page template once...
--Dave 🙂
October 17, 2014 at 5:32 am #128123Grégoire Noyelle
MemberHi Dave
Thanks a lot. Good to see you here 🙂I try but It do not work. And I want to avoid a custom page template for that.
I wonder if it comes from the archive-gnpost_evenement.php (archive-$posttype.php) template?Cheers
October 17, 2014 at 5:44 am #128126David Decker
MemberWhat does if you try directly:
'paged' => get_query_var( 'paged' )
?
October 17, 2014 at 5:47 am #128127Nick
ParticipantHave you tried flushing the WordPress rewrite rules by going to Settings > Permalinks and clicking the "Save Changes" button? Sometimes that is required to get pagination to work with custom post types.
October 17, 2014 at 5:51 am #128128Grégoire Noyelle
Member@david-decker Same result
@Nick Thanks for the help. That's the first thing I make 🙂October 17, 2014 at 6:01 am #128130Grégoire Noyelle
MemberI do think that's a conflict with the archive-$posttype.php model.
I made a lot of genesis custom loop in custom template and It works perfect. But now I need to have more logical url and use the custom post type archive model without creating a new page.
October 17, 2014 at 6:03 am #128131Nick
ParticipantCould it be related to this issue here, perhaps? https://wordpress.org/support/topic/explanation-and-workaround-for-error-404-on-category-pagination?replies=10
If you set Settings > Reading > Blog pages show at most to "1", does the problem go away? http://d.pr/i/1334r
October 17, 2014 at 6:45 am #128136Grégoire Noyelle
MemberThanks a lot @Nick
I just have 20 post on this custom post type and the general setting for Setting > Reading was on 24. You're right 🙂
But to make it works, i have to change theorderby
parameter and removepaged
parameter and add it again.
And If I try to put Setting > Reading on 18, the pagination still works except for the last one, I get a 404.Finaly It always works if I put Setting > Reading on 1. But that's not the good solution for all the website archive.
Really odd. It seems to be very unstable. I hope to find a better solution to handle it.
October 17, 2014 at 7:06 am #128138Grégoire Noyelle
MemberWith a custom template and the exact same $wp_query, I have no problem at all but for me the URL result is just so "bad" and you need a blanck page.
What a shame like genesis supportgenesis-cpt-archives-settings
October 17, 2014 at 4:32 pm #128199Nick
ParticipantDoes it work if you use the pre_get_posts filter in your template? Something like this before your main loop function:
add_filter( 'pre_get_posts', 'my_archive_query' ); function my_archive_query( $query ) { $query->set( 'posts_per_page', 20 ); }
(You could then remove the 'posts_per_page' parameter from the array in your main loop.)
There's an interesting WordPress trac ticket here that you may like to read: https://core.trac.wordpress.org/ticket/16168 It suggests that pagination in archive template loops only works if the query is modified using the pre_get_posts filter.
October 18, 2014 at 7:17 am #128246Grégoire Noyelle
MemberGood catch Nick. Thanks a lot to take the time for that.
I try, and it change nothing. The only case which works perfect is when I set up Settings > Reading > Blog pages show at most to “1″
Not really usefull for all the other archive page.October 18, 2014 at 7:34 am #128250Grégoire Noyelle
MemberI try to make a page with a custom template with the same loop and the same slug as CPT
gnpost_evenement
.
But even, if I delete the file archive-gnpost_evenement.php (archive-$posttype.php) the theme take theindex.php
file as template and note the page 🙁October 18, 2014 at 7:56 am #128252Genesis Developer
Membertry this once
<?php // custom loop for cpt archive remove_action( 'genesis_loop', 'genesis_do_loop'); add_action('genesis_loop','gn_loop_event'); function gn_loop_event() { $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $args = array( 'post_type' => 'gnpost_evenement', 'posts_per_page' => 4, 'paged' => $paged ); global $wp_query; $wp_query = new WP_Query( $args ); if (have_posts() ) : while ( have_posts() ) : the_post(); $boucle = new BBPostEvent('event-grid'); endwhile; do_action( 'genesis_after_endwhile' ); endif; wp_reset_query(); } // function gn_loop_event() genesis();
October 18, 2014 at 1:35 pm #128269Grégoire Noyelle
Memberthanks @genwrock I try but it change nothing.
I make some other tests today.
* I swich to genesis sample to be sure to not influence the theme with other actions
* I add the same archirve-$posttype.php with custom in genesis sample (all the CPT are made with a home made plugin)
* I try pagination as before and I get the same bugs. 404, problem if Settings > Reading > number is more important thant the total of CPT
* If I remove the custom loop, everything is normalI don't know if this is a Genesis Bug or WordPress one. But that anoying.
It will be ok, if I can use a page with the same slug (thant the cpt archive) to make a custom loop. But as I said, If I delete the archirve-$posttype.php, that's the index.php which have stronger priority.
October 18, 2014 at 2:33 pm #128281Grégoire Noyelle
MemberHi
I find the solution. It works great even if the setting for archive is more important than the number of CPT.
I hope It can help.Here is the solution:
For the CPT (gnpost_evenement) archive page (archive-gnpost_evenement.php) I put:
<?php // custom loop for cpt archive remove_action( 'genesis_loop', 'genesis_do_loop'); add_action('genesis_loop','gn_loop_event'); function gn_loop_event() { $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $args = array( 'post_type' => 'gnpost_evenement', 'orderby' => 'title', 'order' => 'DESC', 'paged' => $paged ); global $wp_query; $wp_query = new WP_Query( $args ); if (have_posts() ) : while ( have_posts() ) : the_post(); $boucle = new BBPostEvent('event-grid'); endwhile; do_action( 'genesis_after_endwhile' ); wp_reset_query(); endif; } // function gn_loop_event() genesis();
The difference here is that I do not put the
posts_per_page
parameter. For that I use a plugin OR the functions.php to make some filter. To make it works, you have to fire up this before caling the template file.// Filter for custom post type gnpost_evenement add_filter( 'pre_get_posts', 'gn_event_archive_filter' ); function gn_event_archive_filter( $query ) { if ( $query->is_post_type_archive( 'gnpost_evenement' ) ) : $query->set( 'posts_per_page', 4 ); endif; }
Special thanks to @david-decker, @Nick and @genwrock
October 18, 2014 at 3:12 pm #128282Nick
ParticipantYep, it looks like filtering the posts_per_page in pre_get_posts is the way to do it! Well done for your perseverance, thanks for posting your solution, and I'm really happy that you managed to solve it!
October 19, 2014 at 2:12 am #128298Grégoire Noyelle
MemberThanks @Nick . You give me the idea about pre_get_posts 🙂
It's opens so many possibilities about CPT archive page with a clean URL -
AuthorPosts
- The topic ‘Make pagingation works on custom post type archive’ is closed to new replies.