posts_per_page wrong results problem
-
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
The topic ‘posts_per_page wrong results problem’ is closed to new replies.