• Hello,

    I’m using a custom post type with a pagination. But some posts are repeated and some are not shown. Can’t figure out what the problem is.

    Only thing I know is that there are missing and dublicated posts on the end of the page break “page 2” / “page 3” / “page 4” etc etc. And that this problem only occurse with the pagination. (show posts 9999 fixes the problem)

    Like this: https://autobruins.nl/problem.jpg
    You can see the seat arona is shown two times on page 4 and 5. This is wrong.

    The pagination on “archive-occasions.php”:

    <div class="paginationholder">
        <div class="paginationinnterholder">                                                              
            <?php 
                $nav = get_the_posts_pagination( array(
                        //'mid_size' => 2,
                        'prev_text' => __( '<i class="fal fa-angle-left"></i>', 'textdomain' ),
                        'next_text' => __( '<i class="fal fa-angle-right"></i>', 'textdomain' ),
                        'screen_reader_text' => __( 'A' )
                    ) );
                $nav = str_replace('<h2 class="screen-reader-text">A</h2>', '', $nav);
                echo $nav;
            ?>
        </div>
    </div>

    here is my custom post type code:

    function tf_post_types()
    {   
        $labels = array(
            'name' =>               'Occasions',
            'singular_name' =>      'Occasion',
            'menu_name' =>          'Occasions',
            'name_admin_bar' =>     'Occasions',
            'all_items' =>          'Alle occasions',
            'add_new' =>            'Nieuwe occasion',
            'add_new_item' =>       'Nieuwe occasion toevoegen',
            'edit_item' =>          'Occasion bewerken',
            'new_item' =>           'Nieuwe occasion',
            'view_item' =>          'Occasion bekijken',
            'search_items' =>       'Occasion zoeken',
            'not_found' =>          'Geen occasions gevonden.',
            'not_found_in_trash' => 'Geen occasions gevonden in de prullenbak.',
            'parent_item_colon' =>  'Parent Page',
        );
        $args = array(        
            'labels'                => $labels,
            'public'                => true,
            'exclude_from_search'   => false,
            'publicly_queryable'    => true,
            'show_ui'               => true,
            'show_in_nav_menus'     => true,
            'show_in_menu'          => true,
            'show_in_admin_bar'     => true,
            'has_archive'           => true,
            'menu_position'         => 5,
            //'menu_icon'             => 'dashicons-calendar',
            'menu_icon'             => '/wp-content/themes/tatof/img/assets/car-icon.svg',
            'hierarchical'          => false,
            'rewrite'               => array( 'slug' => 'occasions','with_front' => false,'feeds' => true ), // The url      /customposts/
            'query_var'             => true,
            'can_export'            => true,
            'supports'              => array( 'title','editor','thumbnail','revisions','excerpt','author'),
            'taxonomies'            => array( 'merk' )
        );
        register_post_type( 'occasions', $args );
    }
    add_action( 'init', 'tf_post_types' );
    • This topic was modified 4 years, 2 months ago by tatof.

    The page I need help with: [log in to see the link]

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator bcworkz

    (@bcworkz)

    You could try looking at the actual SQL used to query the posts for that page. It can be had from the global $wp_query object examined just before the template loop runs. Specifically the WP_Query::request property. It sounds like the OFFSET clause is incorrect.

    Some other part of the SQL may give you a clue about the source of the error. It’s maybe due to your theme or a plugin inadvertently altering your post type’s query when it shouldn’t. Try isolating the cause by deactivating all plugins and switching to a default twenty* theme. You’ll need to temporarily copy relevant code and template files to which ever theme you use.

    In a nearly default state, the query should work as expected. Start restoring your theme and plugins, one at a time, until things go wrong again. The last activated module would be the cause.

    Thread Starter tatof

    (@tatof)

    Hello Bcworkz,

    Thanks for your reply! The theme is custom made (by me) and I didn’t set an offset for the pagination/query. But I will look into that! thanks.

    The weird part is that it only occurse when the pagination is active. And exactly on every page break. I’ve got 61 posts “cars” and the query returns 61 posts only not always correct ones. (sounds indeed like an offset or something)

    The problem is that the query is in “nearly default state”, cause its my own built theme. Even when I disable all the plugins the problem still exists. Switching to “twenty* theme” doesnt work because its an Custom Posttype. Turning everything off in my functions.php also doesnt work.

    So I think its save to say something is wrong with the pagination.

    Here is the complete “archive-occasions.php”, maybe you can find something

    <?php get_header(); ?>
    
        <?php if (have_posts()) : ?>
    
            <div class="content100 archive-occasions100">
                <div class="content archive-occasions">
                    
                    <?php
                        if(!empty(get_queried_object()->term_id)){
                            $curTermId = get_queried_object()->term_id;
                        }
                    ?>
                    <h1 class="thetitle">Al onze occasions</h1>
                    
                    <?php
                    $allterms = get_terms('merk');
    
                    if($allterms){ ?>
    
                        <div class="archive-filter">           
                            <div class="filterholder">
                                <a href="/occasions/" class="active"><i class="far fa-cars"></i> Alle merken</a>
                                <?php foreach($allterms as $term){ ?>                                                        
                                    <a href="<?php echo get_term_link( $term );?>" class="<?php if($curTermId === $term->term_id){echo 'active';} ?>">
                                        <i class="far fa-car"></i> <?php echo ucfirst(strtolower($term->name));?>
                                    </a>
                                <?php } ?>                    
                            </div>
                        </div>           
    
                    <?php } ?>                
                    
                    <div class="occasions-feed">
                        
                        <?php while (have_posts()) : the_post(); ?>                
                            <a href="<?php the_permalink();?>" class="occ-item">
                                <div class="theimg">
                                    <img src="<?php echo the_post_thumbnail_url('m-thumb');?>" alt="<?php the_title();?>" loading="lazy">                                
                                </div>
                                
                                <div class="thetxt">
    
                                    <div class="posfix">
                                        <span class="merk">
                                            <?php                                        
                                                $theterm = get_the_terms( $post->ID, 'merk');                
                                                echo ucfirst(strtolower($theterm[0]->name));                                        
                                            ?>                                
                                        </span>
    
                                        <h2><?php the_title();?></h2>
    
                                        <?php if(get_field('occ_spe_bouwjaar')){ ?>
                                            <div class="info">
                                                <i class="fal fa-calendar-alt"></i> <?php echo get_field('occ_spe_bouwjaar');?>
                                            </div>                                   
                                        <?php } ?>
                                        <?php if(get_field('occ_spe_transmissie')){ ?>
                                            <div class="info">
                                                <i class="fal fa-retweet"></i> <?php echo get_field('occ_spe_transmissie');?>
                                            </div>                                   
                                        <?php } ?>                                
                                        <?php if(get_field('occ_spe_kilometerstand')){ ?>
                                            <div class="info">
                                                <i class="fal fa-tachometer-alt-slow"></i> <?php echo get_field('occ_spe_kilometerstand');?>
                                            </div>                                   
                                        <?php } ?>                                
                                        <?php if(get_field('occ_spe_brandstof')){ ?>
                                            <div class="info">
                                                <i class="fal fa-burn"></i> <?php echo get_field('occ_spe_brandstof');?>
                                            </div>                                   
                                        <?php } ?>
                                        <?php if(get_field('occ_spe_motorinhoud')){ ?>
                                            <div class="info">
                                                <i class="fal fa-car"></i> <?php echo get_field('occ_spe_motorinhoud');?>
                                            </div>                                   
                                        <?php } ?>
                                    </div>
                                                                    
                                    <div class="price-holder">                                                                       
                                        <?php if(get_field('occ_prijs')){
                                            if(get_field('occ_prijs') === 'Verkocht' || get_field('occ_prijs') === 'Gereserveerd'){
                                                echo '<div class="price">'.get_field('occ_prijs').'</div>';
                                            }else{
                                                echo '<div class="price">&euro; '.get_field('occ_prijs').'</div>';
                                            }                    
                                        }?>                                              
                                        <div class="btn-moreinfo">Meer informatie</div>                                        
                                    </div>
                                                                    
                                </div>
                            </a>                    
                        <?php endwhile; ?>
                                            
                    </div>
                    <div class="paginationholder">
                        <div class="paginationinnterholder">                                                              
                            <?php 
                                $nav = get_the_posts_pagination( array(
                                        //'mid_size' => 2,
                                        'prev_text' => __( '<i class="fal fa-angle-left"></i>', 'textdomain' ),
                                        'next_text' => __( '<i class="fal fa-angle-right"></i>', 'textdomain' ),
                                        'screen_reader_text' => __( 'A' )
                                    ) );
                                $nav = str_replace('<h2 class="screen-reader-text">A</h2>', '', $nav);
                                echo $nav;
                            ?>
                        </div>
                    </div>
                    
                    <?php if(get_field('occasions_text', 'option')){?>
                        <div class="occassions-text wysiwyg">
                            <?php the_field('occasions_text', 'option');?>
                        </div>
                    <?php }?>
                </div>
            </div>
    
           
    
        <?php endif; ?>                        
    
    <?php get_footer(); ?>
    Thread Starter tatof

    (@tatof)

    Hello,

    Don’t know why but I’ve set these values and the problem is fixed:

    <?php     
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $display_count = get_option( 'posts_per_page' );
        $page_offset = ($paged - 1) * $display_count;
        $args = array('posts_per_page' => $display_count, 'post_type' => 'occasions', 'paged' => $paged, 'offset' => $page_offset );
        query_posts($args); 
    ?>

    Really would like to know why I have to calc the offset myself and the default WordPress function doesnt work.

    Moderator bcworkz

    (@bcworkz)

    Well, yeah. You’ve overridden the pagination offset WP does so it had better fix the problem πŸ™‚ I couldn’t say offhand what causes the problem to start with. Does your theme do anything with post queries of any sort? Like something meant for other posts that has leaked into the occasions query? The default post pagination usually works as it should unless something starts messing with the default query.

    FYI, when you set “offset”, it disables anything related to “paged”, so you don’t really need to specify “paged” anymore. Altering the query with query_posts() is not best practice. It’s inefficient because it forces WP to redo the query it has already done. It’s better to alter the original query through the “pre_get_posts” action. You can set “offset” and “posts_per_page” there just as well. Such an action callback does need to conditionally check if it’s altering the right query since all posts queries pass through such a callback.

    Thread Starter tatof

    (@tatof)

    Its a fix! 😁

    Only thing I can think of messing with the query would be this part in the functions.php for adding extra columns at the admin panel. Only I disabled everything in functions.php so thats not it..

    
    function add_acf_columns ( $columns ) {
       return array_merge ( $columns, array ( 
         'prijs' => 'Prijs'
       ) );
     }
     add_filter ( 'manage_occasions_posts_columns', 'add_acf_columns' );
    
     function exhibition_custom_column ( $column, $post_id ) {
        switch ( $column ) {
            case 'prijs':
                if(get_field( 'occ_prijs',$post_id) === 'Verkocht' || get_field( 'occ_prijs',$post_id) === 'Gereserveerd'){
                    echo get_field( 'occ_prijs',$post_id);
                }else{
                    echo '&euro;'.get_field( 'occ_prijs',$post_id);
                }
                break;  
        }
     }
     add_action ( 'manage_occasions_posts_custom_column', 'exhibition_custom_column', 10, 2 );

    A thanks, I will remove the paged var. I thought the same! I’m changing the “fix” to pre_get_posts and put it in the functions.php.

    An other weird part is that the custom taxonomy I’ve made for this custom post type with the same pagination doesn’t have this problem. (exaclty the same code)

    Anyhow Thanks for your help!😁πŸ’ͺ🏼 But I’m lost in the wonders of random code “errors”, I’ll take this fix as an solution. And I hope the next site I develop will not have the same issue.

    Moderator bcworkz

    (@bcworkz)

    You’re welcome. Another thought occurred to me when you mentioned no issues with the same code with another taxonomy. Maybe it’s not a code issue at all but slightly corrupted data in the taxonomy tables. If the count field for a particular term is off, maybe that would throw off pagination somehow. Not entirely logical, count could maybe affect the final page’s existence, but not intermediate pagination AFAIK. The fact it’s broken to start with isn’t logical either. At least you have a successful workaround.

    Thread Starter tatof

    (@tatof)

    Mabye thats it! The custom post type + custom taxonomy are both beeing imported by an .CSV import with WP all import. Could be something broke the data!

    Just for the fun of it… going to look into that 😁
    Again thanks for the help

    Hello @tatof @bcworkz,

    In my case, I’m using a custom post type created by the Pods Plugin.
    The custom posts are created from an external resource using the REST API, so I have posts created on the exact second, lets say, 10 posts are created on the same second.

    As WP Query has a default orderby date parameter, my pagination gets messed up, repeating some of the last items on the next page at the first positions.

    I have fixed this by passing a custom parameter to the WP Query like orderby ID. This fixed the issue in my case.

    For others with the same problem, I have inspected the SQL generated by the query and diagnose it.

    You can use <?php echo $GLOBALS['wp_query']->request; ?> in your template or for example in functions.php use:

    
    add_filter( 'posts_request', 'dump_request' );
    function dump_request( $input ) {
        var_dump($input);
        return $input
    }
    

    Hope this helps anyone else having the same issue.

    • This reply was modified 4 years, 1 month ago by Daniel P.. Reason: grammar
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Custom posttype archive – pagination – Post missing / repeating’ is closed to new replies.