WordPress.org

Ready to get started?Download WordPress

Forums

Custom Post Type UI
[resolved] Problem with Pagination for Custom Post Types (12 posts)

  1. cirtoska
    Member
    Posted 1 year ago #

    I have seen many had problems with pagination for Custom Post Types and most of them have successfully solved their issue. I still have no luck in that.

    Here is my problem.

    I'm using Custom Post Type UI for creating Custom Post types ( for example one of the post types that I've created is articles ).
    For listing them I have created Page Template articles.php and the slug that I'm using for that page is article ( for example the page where I have created is listed here http://esotericquotes.com/article/ ).

    The first page is listing fine, but when I go to the next page it redirect to the first page and gives the permalink of the second page http://esotericquotes.com/article/page/2/

    I'm using Maxx - Responsive Creative WordPress Theme and the code for the pagination for this theme looks like this:

    /*Custom Pagination
    /*---------------------------------------------------------------------------------------------*/
    if ( ! function_exists( 'md_pagi_nav' ) ) :
    	function md_pagi_nav(array $query = array()) {
    	?>
    	<div class="m-pagination">
    			<?php
                    global $wp_query;
    
                    $big = 999999999; 
    
                    echo paginate_links( array(
                        'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
                        'format' => '?paged=%#%',
                        'current' => max( 1, get_query_var('paged') ),
                        'total' => $wp_query->max_num_pages,
                        'next_text'    => __('Next →','framework'),
                        'prev_text'    => __('← Prev','framework')
                    ) );
                ?>
                <span class="pagination-meta">
                    <?php
                        //display Page x of y pages
                        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                        echo 'Page ' . $paged . ' of ' . $wp_query->max_num_pages ;
    
                    ?>
                </span>
    
            </div>
    
            <?php
    	}
    endif;

    Can someone with more experience in coding help me how to solve this issue please?

    http://wordpress.org/extend/plugins/custom-post-type-ui/

  2. Michael Beckwith
    Member
    Plugin Author

    Posted 1 year ago #

    Still having any issues with this one?

  3. cirtoska
    Member
    Posted 1 year ago #

    Yes

  4. Michael Beckwith
    Member
    Plugin Author

    Posted 1 year ago #

    You said you're using a page template for the Custom Post Type display, articles.php. I'm guessing you have something like "Template Name: Articles" at the top in a php comment block, correct? For my reference, if you could pastebin the articles.php file, that'd be awesome.

    What code are you using to display the post type? Grabbing the global $wp_query variable on a page is going to pull the variable for the page's query, not the post type's

    That's my first set of questions, I'll be able to help a bit better after that.

  5. cirtoska
    Member
    Posted 1 year ago #

    Thanks for your feedback Michael,

    1. Yes. I'm using "Template Name: Articles", here is a pastebin for articles.php

    2. The code that I'm using for displaying the post type
    <?php query_posts( 'post_type=articles'); ?>

  6. Michael Beckwith
    Member
    Plugin Author

    Posted 1 year ago #

    I'd honestly start by using one of the template files indicated here: http://codex.wordpress.org/Template_Hierarchy#Custom_Post_Types_display

    There are dedicated template file names for custom post types, specifically archive-$cpt_name.php

    Once you have that part in place, I'd see how far along you are with the removal of your query_posts() attempt, which is not a good way to try and achieve this, for performance reasons among others.

  7. cirtoska
    Member
    Posted 1 year ago #

    Thank you Michael for your support.

    I found the solution! I have created as you suggested archive-articles.php and instead <?php query_posts( 'post_type=articles'); ?> I replaced it with

    <?php function articles_filter($query) {
      if ( !is_admin() && $query->is_main_query() ) {
        if (is_page_template('archive-articles.php')) {
          $query->set('post_type', 'articles');
        }
      }
    }
    
    add_action('pre_get_posts','articles_filter'); ?>

    Now the navigation works!

    Cheers!
    Aleksandra

  8. Michael Beckwith
    Member
    Plugin Author

    Posted 1 year ago #

    Awesome to hear. Shouldn't have need for that pre_get_posts filter logically, but I'm not going to argue against something working at this point.

  9. cirtoska
    Member
    Posted 1 year ago #

    Well if you have any other suggestion we can try it also. I'm willing to try everything for better performance of my custom post types.

    Also I have another suggestion: In Advanced Options for Custom Post Types the attribute Has Archive is by default False. If people use archive-$cpt_name.php for displaying custom post types they should manually change this option to True.

  10. Michael Beckwith
    Member
    Plugin Author

    Posted 1 year ago #

    I don't believe "has_archive" actually has any effect on the template hierarchy. A bit confusing in the end because of that, but archive-$cpt.php is first in line for CPTs regardless of what features its registered with.

  11. cirtoska
    Member
    Posted 1 year ago #

    No "has-archive" doesn't have any effect on the template hierarchy. I was mentioning that "has_archive" should be by default "True", not "False" for it to display archive from the custom post type.

    Anyway thanks again for your time

    Best Regards,
    Aleksandra :)

  12. Michael Beckwith
    Member
    Plugin Author

    Posted 1 year ago #

    I think I get what you mean now. It's been a tired day.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic