• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Make pagingation works on custom post type archive

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › General Discussion › Make pagingation works on custom post type archive

This topic is: resolved

Tagged: custom loop, Custom Post Type, custom post type archive

  • This topic has 18 replies, 4 voices, and was last updated 7 years, 10 months ago by Grégoire Noyelle.
Viewing 19 posts - 1 through 19 (of 19 total)
  • Author
    Posts
  • October 17, 2014 at 1:17 am #128098
    Grégoire Noyelle
    Member

    Hi

    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: visit

    Could you help me?

    Thanks
    Grégoire

    http://treow.fr/dev/boumbang/evenement/
    October 17, 2014 at 1:30 am #128103
    Grégoire Noyelle
    Member

    I forget to mention that the real name of the custom archive page is : archive-gnpost_evenement.php

    October 17, 2014 at 5:26 am #128122
    David Decker
    Member

    Cou 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 🙂


    Twitter @deckerweb // Google+ // My Genesis & WordPress Plugins // German Translations for Genesis

    October 17, 2014 at 5:32 am #128123
    Grégoire Noyelle
    Member

    Hi 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 #128126
    David Decker
    Member

    What does if you try directly: 'paged' => get_query_var( 'paged' ) ?


    Twitter @deckerweb // Google+ // My Genesis & WordPress Plugins // German Translations for Genesis

    October 17, 2014 at 5:47 am #128127
    Nick
    Participant

    Have 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 #128128
    Gré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 #128130
    Grégoire Noyelle
    Member

    I 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 #128131
    Nick
    Participant

    Could 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 #128136
    Grégoire Noyelle
    Member

    Thanks 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 the orderby parameter and remove paged 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 #128138
    Grégoire Noyelle
    Member

    With 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 support genesis-cpt-archives-settings

    October 17, 2014 at 4:32 pm #128199
    Nick
    Participant

    Does 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 #128246
    Grégoire Noyelle
    Member

    Good 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 #128250
    Grégoire Noyelle
    Member

    I 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 the index.php file as template and note the page 🙁

    October 18, 2014 at 7:56 am #128252
    Genesis Developer
    Member

    try 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();

    Download Genesis Featured Posts Combo Widget | Simple Grid Layouts Plugin for Posts, CPTs and terms
    You can request new tips/help.

    October 18, 2014 at 1:35 pm #128269
    Grégoire Noyelle
    Member

    thanks @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 normal

    I 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 #128281
    Grégoire Noyelle
    Member

    Hi
    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 #128282
    Nick
    Participant

    Yep, 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 #128298
    Grégoire Noyelle
    Member

    Thanks @Nick . You give me the idea about pre_get_posts 🙂
    It's opens so many possibilities about CPT archive page with a clean URL

  • Author
    Posts
Viewing 19 posts - 1 through 19 (of 19 total)
  • The topic ‘Make pagingation works on custom post type archive’ is closed to new replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2022 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble