• Hi there.

    I have a post category which has 22 sample posts. I created a shortcode with wp_query which has some criterias, like sorting by post id etc. I will create a few similar shortcodes for another post type or another templated content or different sorting types (sorting by custom field) etc. So I need fix this posts_per_page issue first.

    In this query I set posts_per_page by 4. in front site, there is 4 post per page. just like I want. but when I click navigation links, between 1st and 4th pages works well but 5th and 6th pages get 404 not found page.

    I put $GLOBALS[‘wp_query’]->request to see query details. Its look like in query its simulating 6 posts per page. its also explains why 5th and 6th page gets not found page ( 4 page x 6 ppp = 24 ). 22 posts filling 4 pages. thats the problem. in pagination links there is still 5th and 6th page links.

    I tried so many ways to fix but get nothing. Need your help.

    Here is wp_query & shortcode code:

    function kategori_yazilari() {
    global $paged;
    $kategori_yazilari = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'orderby'=> 'ID', 'category__in' => get_query_var( 'cat' ), 'posts_per_page' => 4, 'paged' => $paged));
    $content = '';
    $content .= '<div class="et_pb_row et_pb_row_2 et_pb_equal_columns et_pb_gutters2 kategori_yazilari">';
    $i = 0;
    while ( $kategori_yazilari->have_posts() ) : $kategori_yazilari->the_post();
    $i++;
    $content .= '<div class="et_pb_column et_pb_column_1_2 et_pb_column_4  et_pb_css_mix_blend_mode_passthrough">
    <div class="et_pb_module et_pb_code et_pb_code_4 blog_yazi_resimli_tekli">
    <article>
    <div class="yazi-gorsel"><a href="'. get_the_permalink() .'">' . get_the_post_thumbnail() . '</a></div>
    <div class="yazi-title"><a href="'. get_the_permalink() .'">' . get_the_title() . '</a></div>
    </article>
    </div>
    </div>';
    if ($i % 2 == 0) {
    $content .= '</div><div class="et_pb_row et_pb_row_2 et_pb_equal_columns et_pb_gutters2 kategori_yazilari">';
    }
    endwhile;
    $content .= '</div>';
    
    $content .= paginate_links( array(
                'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
                'total'        => $kategori_yazilari->max_num_pages,
                'current'      => max( 1, get_query_var( 'paged' ) ),
                'format'       => '?paged=%#%',
                'show_all'     => true,
                'type'         => 'plain',
                'end_size'     => 2,
                'mid_size'     => 1,
                'prev_next'    => false,
                'add_args'     => false,
                'add_fragment' => '',
            ) );
    
    wp_reset_postdata();
    return $content;
    }
    add_shortcode('kategori_yazilari', 'kategori_yazilari');

    and here is $GLOBALS[‘wp_query’]->request result:

    SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (3) ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 6
    posts wp-query pagination
Viewing 2 replies - 1 through 2 (of 2 total)
  • A shortcode that does pagination needs to have its own variable for it instead of using the WP page variables. This page shows the parameters https://developer.ww.wp.xz.cn/reference/classes/wp_query/

    Since a shortcode can go anywhere, even more than once on a page, it can’t rely on the main query of the page that it’s on for the paging.
    You would do better to leave the pagination to WordPress and only output what fits on one page.
    But it seems that you are using a shortcode in a strange way. Perhaps what you need instead of a shortcode is a search interface so that the user can filter the existing category archive pages with the criteria they want. There are plugins that do this for taxonomies (like for products in Woocommerce) but also lots related to Advanced Custom Fields plugin.

    Thread Starter ezmana

    (@ezmana)

    first of all, thank you for your reply.

    yeah its like a little mess but I have to use a theme (Divi theme) which actually has “blog posts module”. custom layout owning category archive page and with their visual builder you can do more efficient designs. but this module design isn’t what client wants 🙂 so I have to create shortcodes for posts, post categories, cpt, cpt category archive etc. even posts search and cpt search page 🙂 and most of them will need pagination and different designs.

    I think its happens because of Divi layout builder. I put same shortcode in standalone page on same website, its works well. 6 pages, 4 posts per page. all 22 posts displays.

    its look like Divi layout builder (or because of them) modifies limit part of query.

    I wrote them, waiting for answer. I hope they will get fixed the problem but if they’re not I have to find some other ways like you said 🙂 thanks again.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘posts_per_page wrong results problem’ is closed to new replies.