0

I'm new here. So if I ask my question in wrong way, let me correct please. Anyway, here is my problem.

I have a post category which has 22 sample posts. I created a shortcode with wp_query which has some criteria, 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 the front site, there is 4 posts 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. It look like in query its simulating 6 posts per page. it also explains why 5th and 6th page gets not found page ( 4 page x 6 ppp = 24 ). 22 posts filling 4 pages. That's 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 .= '';
    $i = 0;
    while ( $kategori_yazilari->have_posts() ) :
        $kategori_yazilari->the_post();
        $i++;
        $content .= '
        
        
        ' . get_the_post_thumbnail() . '
        ' . get_the_title() . '
        
        
        ';
        if ($i % 2 == 0) {
            $content .= '';
        }
    endwhile;
    $content .= '';
    
    $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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.